[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Efficient Octave Code
From: |
Daniel Oberhoff |
Subject: |
Re: Efficient Octave Code |
Date: |
Wed, 25 Oct 2006 11:15:13 +0200 |
User-agent: |
Thunderbird 1.5.0.7 (X11/20060915) |
One of the strong points of octave (and Matlab) is vectorization,
meaning it will shine if you issue operations on whole arrays at once.
Since your loops go over the whole array you can omit the loops and the
indexes and just write:
Fk = F .* (w == k);
octave is smart enough to figure that it should apply the scalaer k to
every element of w here. Most of "octaves notation" is just matrix
notation btw. (above the .* is important meaning elementwise mult,
otherwise you would have had a regular matrix mult).
Cheers
Daniel
rockster8 wrote:
> Hi guys,
> I'm still trying to get used to octave's notations. I'm so used to coding in
> c++ that i find it really hard transitioning to octave. I've got a nested
> for-loop and was wondering if there is an octave-equivalent notation that
> will simplify the for-loop:
>
> for i=1:50
> for j=1:50
> for k=1:10
> Fk(i,j) = F(i,j) .* (w(i,j) == k);
> end
> end
> end
>
> where the following are initialized as:
> Fk(1:50,1:50) = 0;
> F(1:50,1:50) = 0;
> w(1:50,1:50) = randomly generated numbers..
>