help-octave
[Top][All Lists]
Advanced

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

Re: function applied to each component in an if clause


From: Michael Goffioul
Subject: Re: function applied to each component in an if clause
Date: Wed, 12 Mar 2008 06:28:45 +0100

On 3/11/08, sururi <address@hidden> wrote:
>
> I'm wondering if there is a practical way of applying a comparison operator
> to each component of a vector..
>
> function [y] = lambda(x)
>  y.=x*2;
> endfunction
>
> x = [1 2 3];
> lambda(x)
>
> ans =
>   2   4   6
>
> this works ok. But something like:
>
> function [y] = lom(x)
>    if(x<2)
>        y=x.*3;
>    else
>        y=x.*1;
>    endif
> endfunction

You can write it as (not tested, but you get the idea):

idx1 = find (x<2);
idx2 = find (x>=2);
y(idx1) = x(idx1)*3;
y(idx2) = x(idx2)*1;

Michael.


reply via email to

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