How do I make the element zero, if its adjacent column element is zero?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Taner Cokyasar
le 10 Juil 2016
Modifié(e) : per isakson
le 12 Juil 2016
Hello,
Assume, I have the following 9x2 matrix. When there is a zero in the second column, I want to make the first column's same row element zero too.
12 0
11 0
10 6
12 0
11 3
10 0
12 0
11 5
10 0
This will become like:
0 0
0 0
10 6
0 0
11 3
0 0
0 0
11 5
0 0
Thank you so much
0 commentaires
Réponse acceptée
per isakson
le 10 Juil 2016
Modifié(e) : per isakson
le 10 Juil 2016
Try
>> is_zero = A(:,2)==0;
>> A(is_zero,1)=0;
>> A
A =
0 0
0 0
10 6
0 0
11 3
0 0
0 0
11 5
0 0
Plus de réponses (1)
Star Strider
le 10 Juil 2016
That can actually be done in one line of code:
M = [12 0
11 0
10 6
12 0
11 3
10 0
12 0
11 5
10 0];
M(M(:,2) == 0,1) = 0
M =
0 0
0 0
10 6
0 0
11 3
0 0
0 0
11 5
0 0
3 commentaires
per isakson
le 12 Juil 2016
Modifié(e) : per isakson
le 12 Juil 2016
Adding a question in a comment to an existing question with an accepted answer doesn't attract many viewers. I think it is better to open a new question.
I have problems understanding your "pseudo indexing".
"Y13 = 1 because Z13 = 1"   Here is a piece of elegant Matlab code. (Not sure it is relevant, though.)
>> A = zeros(3,4,2);
>> B = randi( [1,2], size(A) );
>> A(B==1)=1; % or A(B==1)=B(B==1);
>> B
B(:,:,1) =
2 2 1 2
1 1 1 2
2 2 2 1
B(:,:,2) =
2 1 1 2
1 1 2 1
1 2 1 2
>> A
A(:,:,1) =
0 0 1 0
1 1 1 0
0 0 0 1
A(:,:,2) =
0 1 1 0
1 1 0 1
1 0 1 0
>>
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!