help-octave
[Top][All Lists]
Advanced

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

Re: loops vs vectorization


From: Robert A.Macy
Subject: Re: loops vs vectorization
Date: Wed, 05 Jul 2006 17:46:21 -0700

On Wed, 5 Jul 2006 18:20:40 -0500
 "Ozzy Lash" <address@hidden> wrote:
> On 7/5/06, Robert A.Macy <address@hidden> wrote:
> > Would somebody be kind enough to refresh my memory how
> to
> > avoid a "for" loop when there are a lot of conditional
> > adjustments going on?
> >
> > for example, the simple act of replacing every item
> that is
> > less than negative with the value zero.
> >
> > Can't remember all the nuances on writing 'vectorized'
> > versions of those types of conversions.
> >
> >              - Robert -
> >
> One way to do what you ask is:
> 
> octave:6> A=rand(5)-0.5
> A =
> 
>    0.172279   0.035958   0.236123   0.472645   0.278473
>   -0.143212  -0.459838   0.450752   0.460015  -0.332292
>    0.126755  -0.047737   0.044301   0.338564  -0.118386
>   -0.245099  -0.466291  -0.498579   0.164236  -0.155360
>   -0.252265   0.239568  -0.298967   0.435878  -0.153951
> 
> octave:7> A(A<0)=0
> A =
> 
>   0.17228  0.03596  0.23612  0.47265  0.27847
>   0.00000  0.00000  0.45075  0.46002  0.00000
>   0.12675  0.00000  0.04430  0.33856  0.00000
>   0.00000  0.00000  0.00000  0.16424  0.00000
>   0.00000  0.23957  0.00000  0.43588  0.00000
> 
> octave:8>

Thanks, that's it?  All these years...doing for loops

you say 'one way' are there others?

so how to do 'if, else' without doing two statements in a
row?

       - Robert -



reply via email to

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