MATLAB: Matrix

arraymatrix

I have a matrix S1 and S2 which consist of +1, -1 and 0.
I want a matrix S such that it is +1 if there is +1 in both S1 and S2. -1 if there is -1 in both S1 and S2. 0 if anything else.

Best Answer

eg
S1 = randi([-1 1],10)
S2 = randi([-1 1],10)
solution
k= S1 + S2
S = (k==2) - (k==-2)
OR
S = (S1 == 1 & S2 == 1) - (S1 == -1 & S2 == -1)
MORE variant
S = (S1 == S2).*S1
or
S = (S1 == S2).*S2