how to get the element >0 in array

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

how to get the element >0 in array

turbofib
i've the following array:

a=[-3 -5 0 5 4]

i want to loop it until i find element>0 (5)


can I loop it but is there a faster method?





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: how to get the element >0 in array

Ian McCallion
find(~a)

On 18 July 2018 at 14:20, turbofib <[hidden email]> wrote:
i've the following array:

a=[-3 -5 0 5 4]

i want to loop it until i find element>0 (5)


can I loop it but is there a faster method?





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html





Reply | Threaded
Open this post in threaded view
|

RE: how to get the element >0 in array

Windhorn, Allen E [ACIM/LSA/MKT]
In reply to this post by turbofib

> -----Original Message-----
> From: Help-octave [mailto:help-octave-
> bounces+allen.windhorn=[hidden email]] On Behalf Of turbofib
>
> i've the following array:
>
> a=[-3 -5 0 5 4]
>
> i want to loop it until i find element>0 (5)
>
> can I loop it but is there a faster method?

>> a=[-3 -5 0 5 4]
a =  -3  -5   0   5   4

>> a(a>0)
ans =

   5   4

>> a(a>0)(1)
ans =  5

Regards,
Allen


Reply | Threaded
Open this post in threaded view
|

Re: how to get the element >0 in array

nrjank
On Wed, Jul 18, 2018 at 9:31 AM, Windhorn, Allen E [ACIM/LSA/MKT] <[hidden email]> wrote:

> -----Original Message-----
> From: Help-octave [mailto:[hidden email]
> bounces+allen.windhorn=[hidden email]] On Behalf Of turbofib
>
> i've the following array:
>
> a=[-3 -5 0 5 4]
>
> i want to loop it until i find element>0 (5)
>
> can I loop it but is there a faster method?

>> a=[-3 -5 0 5 4]
a =  -3  -5   0   5   4

>> a(a>0)
ans =

   5   4

>> a(a>0)(1)
ans =  5

Regards,
Allen



used for a loop: 

for i = a(a<0)
 i
endfor

i = -3
i = -5


if you also want the zero:

for i = a(a<=0)
 i
endfor

i = -3
i = -5
i = 0