Matrix

2 vues (au cours des 30 derniers jours)
Mate 2u
Mate 2u le 12 Avr 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.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 12 Avr 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

Plus de réponses (1)

Wayne King
Wayne King le 12 Avr 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;

Catégories

En savoir plus sur Language Fundamentals dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by