## Faster multiple matrix value assignments in MATLAB

You can post your problem related to MATLAB Projects here. We will try our best to help you out.
Charles
Posts: 20
Joined: Sat Feb 18, 2017 7:12 am

### Faster multiple matrix value assignments in MATLAB

I want to calculate the norm of each column of a large matrix (tens rows with thousands of columns) containing NaN. Prior to calculating the norm, each column is subtracted by its mean. All NaN values are treated as 0. Thus I perform these as

Code: Select all

``````nanix = isnan(X);
nx = sum(~nanix); % count the number of non-NaN values in each column for calculating mean
X(nanix) = 0;
X = bsxfun(@minus, X, sum(X)./nx);
X(nanix) = 0;
xnorm = sqrt(sum(X.^2));``````
I think this is efficient except the two lines which assign all NaN values to 0. profile shows that these two lines cost more than 50% of all computations. Using my data matrix of size 70 by 2000, more than 10 s is spent for running the assignments for 10,000 times. Any suggestion?

===============

As requested, a test function can be:

Code: Select all

``````%%
function test
a = randn(80,3000);
[r,c] = size(a);
b = randperm(r*c);
nanix = b(1:round(numel(b)*0.3)); % randomly select 30% of values to be NaN
a(nanix) = NaN;
nnx = sum(~isnan(a));
tic;
for i = 1:1000
t=a;
t(nanix)=0;
tm = sum(t)./nnx;
t = bsxfun(@minus, t, tm);
t(nanix) = 0;
tnorm = sqrt(sum(t.^2));
end
tt = toc;
fprintf('time: %.4f',tt);
end``````
The output is

Code: Select all

``````>> test
time: 3.4734``````
profile shows that the first t(nanix)=0; costs 42% and second costs 16.3% of total running time.

Heraldo
Posts: 10
Joined: Sun Jun 16, 2019 6:57 am

### Re: Faster multiple matrix value assignments in MATLAB

Excellent information for education. But I'm looking for a writing service. What do you recommend?

Jamalun
Posts: 16
Joined: Wed May 29, 2019 7:35 am

### Re: Faster multiple matrix value assignments in MATLAB

I could never learn to be an engineer. because I have a hard time dealing with academic papers. Fortunately, my student life has become much easier at the moment thanks to qualified canadian writers. I can order any paper cheap and always sure of the quality.