help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Matrix loop


From: James Sherman Jr.
Subject: Re: Matrix loop
Date: Wed, 5 May 2010 14:23:12 -0400

I'm sure there is a more efficient/elegant way of doing this, but here's my stab at it:

s_cR = 21;
n = 1000;
a = rand(s_cR, s_cR, n);
meant = repmat(mean(a,3), [1 1 n]);
b = mat2cell(a - meant, s_cR, s_cR, ones(1, n));
f = @(x) (x*x');

c = cell2mat(cellfun(f, b, 'UniformOutput', false));

result = sum(c, 3)/n;
======
Note: I can only test this in matlab currently, but I'm pretty sure there's no compatibility issues with these functions.

In english, what I'm doing to avoid the loop is to first use repmat to make a copy of the meant for each tt value then subtracting.  Then, split the 3 dimensional array into a cell array with entries that are 2 dimensional.  Then use the cellfun call to apply the function f to each element of the cell array, and then use cell2mat to put it back into an array.


On Wed, May 5, 2010 at 12:30 PM, Riccardo Corradini <address@hidden> wrote:
Hi,
I would know if is there a more efficient way to perform the following operation

s_cR =21;
n=1000;
a= rand(s_cR,s_cR,n);
whos a
meant = mean(a,3);
whos meant
for tt=1:n
    b(:,:,tt) = a(:,:,tt) - meant;
    c(:,:,tt)= b(:,:,tt) * b(:,:,tt)';
endfor
result = sum(c,3)*1/n;
whos result

Thanks in advance


_______________________________________________
Help-octave mailing list
address@hidden
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave



reply via email to

[Prev in Thread] Current Thread [Next in Thread]