help-octave
[Top][All Lists]
Advanced

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

Re: Vectorizing simple loops


From: Francesco Potortì
Subject: Re: Vectorizing simple loops
Date: Thu, 03 Dec 2015 15:14:51 +0100

>Brian - sure, you are welcome. I learned a lot to reuse later too. Indeed the
>solution is very elegant, I never suspected such would be possible. I am
>pasting it here too, for reference:
>
>in = [ 1 0 2 0 7 7 7 0 5 0 0 0 9 ]
>out = nonzeros(in).'(cumsum(in~=0))

Since this is a help list, here is a hint for those that are not
proficient in Octave.

The .' operator in this case (real numbers) is the same as the '
(transpose) operator, and is only needed to obtain a horizontal vector.

So this is equivalent to:

n = nonzeros(in)'
out = n((cumsum(in!=0))

-- 
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it




reply via email to

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