Element wise if then in vector

Hi,
I have searched for this but found no answer. I have two arrays M1 and M2 , each with million+ elements.
I want to implement this logic and create a new vector S from each element of M1 and M2
S (i) = 0 if abs(M1(i)-M2(i))< Theta S(i) = sign(M1(i)-M2(i)) otherwise
For loops take forever on this. Any solution that makes it more vector operations would be great.
Vamsi

Respuestas (1)

Vamsi
Vamsi el 26 de Nov. de 2013

0 votos

I seem to have hit the answer, please do let me know if there is an issue I didn't see. created a T = M1-M2 , initiate S = zeros(length(M1),1).
Then S(ABS(T)>Theta)=sign(T(ABS(T)>Theta))

2 comentarios

Simon
Simon el 26 de Nov. de 2013
That's it!
T = M1 - M2;
S = zeros(numel(M1),1);
l = abs(T) >= Theta;
S(l) = sign(T(l));

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 26 de Nov. de 2013

Comentada:

el 26 de Nov. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by