vendredi 30 janvier 2015

Possible Vectorization using If Statements in MATLAB

Suppose I have the following column vectors as



res = 0.81 res2 = -0.28
0.78 -0.41
0.85 -0.62
0.50 -0.56
0.63 -0.11


and I have two fixed constant D = 0.5. Now suppose an element of res1 is called X and an element of res2 is called Y. I have the following conditions



if (X > D && Y < -D)
output = 1
elseif (X < -D && Y > D)
output = -1
else
output = 0
end


My question is this:


Is it possible to "vectorize" these conditions to iterate over the entire vectors res1 and res2, such that my output vector would give (for example) :



output = -1
0
-1
0
1


?


I know I can do it via a loop, but I would prefer to avoid it since these vectors are actually quite large (>10000). I have attempted to use logical indexing, but to no avail (unless I'm implementing it wrong).


Any help would be appreciated!


Aucun commentaire:

Enregistrer un commentaire