Matrix

2 views (last 30 days)
Mate 2u
Mate 2u on 12 Apr 2012
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.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 12 Apr 2012
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

More Answers (1)

Wayne King
Wayne King on 12 Apr 2012
One way
x = [1 -1; 1 -1];
y = [1 1; 1 -1];
Z= zeros(size(x));
Indx1 = find(x==1);
Indy1 = find(y==1);
Indxneg1 = find(x==-1);
Indyneg1 = find(y==-1);
Ind1 = intersect(Indx1,Indy1);
Z(Ind1) = 1;
Indneg1 = intersect(Indxneg1,Indyneg1);
Z(Indneg1) = -1;

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!