lundi 3 juillet 2017

Matlab: change vector values based on if-else condition on the same vector

I have a threshold requirement and would like to round values of a vector to 0 or 1. I don't need to save the old values of the vector

I saw, in the link below, that I could use the following to change the all values of 0 in the s vector to -1 with s( s==0 )=-1; or in general vector(if condition) = desiredValue.

This is better than using a for loop and having an enclosed if condition. What if I would like to include another condition: to change all values greater than 0.8 to 1? Would I have to add another line of code:s( s>=0.8 )=1; or is there a way of checking the two conditions by traversing the vector just once? I only know of using a for loop with an enclosed if-else condition but would like a shorter version if one exists.

http://ift.tt/2tKvotI

The only related link I found was for R but I am not working with it: Change vector value based on value in same vector

Aucun commentaire:

Enregistrer un commentaire