help-octave
[Top][All Lists]
Advanced

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

Re: how to get the element >0 in array


From: Nicholas Jankowski
Subject: Re: how to get the element >0 in array
Date: Wed, 18 Jul 2018 09:38:46 -0400

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

> -----Original Message-----
> From: Help-octave [mailto:help-octave-
> bounces+allen.windhorn=mail.address@hidden] 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

you should look up Indexing

https://octave.org/doc/v4.4.0/Index-Expressions.html

reply via email to

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