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.
Post Reply
Charles
Posts: 20
Joined: Sat Feb 18, 2017 7:12 am

Faster multiple matrix value assignments in MATLAB

Post by Charles » Mon Mar 06, 2017 12:37 pm

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.

Post Reply