help-octave
[Top][All Lists]
Advanced

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

Re: Vectorizing code


From: Jordi Gutiérrez Hermoso
Subject: Re: Vectorizing code
Date: Sat, 24 Sep 2011 17:19:44 -0500

On 24 September 2011 13:38, 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

Well, since matrix multiplication works column by column...

    b = A\reshape(a, size(a,1)*size(a,2), 3)';
    a = reshape(b', size(a))

HTH,
- Jordi G. H.


reply via email to

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