[Scilab-users] Conditional assignments

classic Classic list List threaded Threaded
4 messages Options
Michael Dunn Michael Dunn
Reply | Threaded
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-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
Stefan Du Rietz Stefan Du Rietz
Reply | Threaded
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
> <http://www.tech.ubm.com/>
>
>
>
>
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.scilab.org/mailman/listinfo/users
>


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Michael Dunn Michael Dunn
Reply | Threaded
Open this post in threaded view
|

Re: Conditional assignments

Very cool. What other tricks can one do with %t & %f?

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
 <http://www.tech.ubm.com/>






-----Original Message-----
From: Stefan Du Rietz <[hidden email]>
Reply-To: "International users mailing list for Scilab."
<[hidden email]>
Date: Sunday, April 21, 2013 4:28 PM
To: "International users mailing list for Scilab." <[hidden email]>
Subject: Re: [Scilab-users] 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
>> <http://www.tech.ubm.com/>
>>
>>
>>
>>
>> _______________________________________________
>> users mailing list
>> [hidden email]
>> http://lists.scilab.org/mailman/listinfo/users
>>
>
>
>_______________________________________________
>users mailing list
>[hidden email]
>http://lists.scilab.org/mailman/listinfo/users

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Stefan Du Rietz Stefan Du Rietz
Reply | Threaded
Open this post in threaded view
|

Re: Conditional assignments

You can combine several index vectors with & and/or(!) |

You can also shift one e.g. to get the preceding elements:
[index(2:$) %f]

Use your imagination ;-)

Regards
Stefan


On 2013-04-21 22:42, Michael Dunn wrote:

> Very cool. What other tricks can one do with %t & %f?
>
> 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
>   <http://www.tech.ubm.com/>
>
>
>
>
>
>
> -----Original Message-----
> From: Stefan Du Rietz <[hidden email]>
> Reply-To: "International users mailing list for Scilab."
> <[hidden email]>
> Date: Sunday, April 21, 2013 4:28 PM
> To: "International users mailing list for Scilab." <[hidden email]>
> Subject: Re: [Scilab-users] 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
>>> <http://www.tech.ubm.com/>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> users mailing list
>>> [hidden email]
>>> http://lists.scilab.org/mailman/listinfo/users
>>>
>>
>>
>> _______________________________________________
>> users mailing list
>> [hidden email]
>> http://lists.scilab.org/mailman/listinfo/users
>
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.scilab.org/mailman/listinfo/users
>

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users