Matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Andrei Bobrov
2012년 4월 12일
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
댓글 수: 0
추가 답변 (1개)
Wayne King
2012년 4월 12일
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;
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!