# [Scilab-users] Conditional assignments

4 messages
Open this post in threaded view
|

## [Scilab-users] Conditional assignments

 Say I want to do an array (vector) assignment, but the formula requires a conditional. Is there a better way to do it than:b = ((a^2).*(a<5)) + ((a^0.5).*(a>=5))which returns a^2 for a<5, and sqrt(a) for a>=5.And – just curious – if this is the only way, is the interpreter smart enough to not waste its time computing the false expression?Thanks,Michael Dunn | Editor-in-ChiefScope Junction | http://www.scopejunction.com/(415) 947-6096 (USA)(519) 744-9395 (Canada)Skype: MichaelDunn_UBMLinkedIn: http://www.linkedin.com/in/cantares _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Conditional assignments

 If the interpreter is not smart enough and the vector is large, maybe this is faster -- only one computation/element: index = a<5; b = zeros(a); b(index) = a(index)^2; b(~index) = a(~index)^0.5; /Stefan On 2013-04-21 21:49, Michael Dunn wrote: -------------------- > Say I want to do an array (vector) assignment, but the formula > requires a conditional. Is there a better way to do it than: > > b = ((a^2).*(a<5)) + ((a^0.5).*(a>=5)) > > which returns a^2 for a<5, and sqrt(a) for a>=5. > > And – just curious – if this is the only way, is the interpreter smart > enough to not waste its time computing the false expression? > > > Thanks, > > > Michael Dunn | Editor-in-Chief > Scope Junction | http://www.scopejunction.com/> (415) 947-6096 (USA) > (519) 744-9395 (Canada) > Skype: MichaelDunn_UBM > LinkedIn: http://www.linkedin.com/in/cantares> > > > > > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users> _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users