help-octave
[Top][All Lists]
Advanced

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

Re: Vectorizing code


From: James Sherman Jr.
Subject: Re: Vectorizing code
Date: Sat, 24 Sep 2011 17:36:34 -0400

On Sat, Sep 24, 2011 at 2:38 PM, Torbjörn Rathsman <address@hidden> wrote:
How do I vectorize this piece of code?

a is an NDArray

for i=1:size(a,1)
       for j=1:size(a,2)
               v=[a(i,j,1) a(i,j,2) a(i,j,3)]';
               v=A\v;
               a(i,j,1)=v(1);
               a(i,j,2)=v(2);
               a(i,j,3)=v(3);
       end
end


Hi,

I don't have time to double check it right now, but I think you might want to use the "arrayfun" function.  Something like:
A = rand(3);
f = @(x,y,z)A\[x;y;z];
b = arrayfun(f, a(:, :,1), a(:,:,2), a(:, :,3), "UniformOutput", false);

Will return a cell array where b(i,j) = a(i,j,:).

Not sure if this works perfectly, but hopefully it at least points you in the right direction.


reply via email to

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