numbering rows and then using these numbers for another vector of equal n dimensions

If I have a matrix for example
A = [ 1 2 3; 1 2 5; 0 55 8; 0 0 0; 7 8 9; 4 0 5; 0 0 0] and I have a vector
B = [ 1; 2; 3; 4; 5; 6; 7].
(please note these are small for simplicity but my actual matrices and vectors are much bigger)
Now say I wanted to record the row numbers at which all three columns contained zeros and then replace the numbers in B at these particular rows with zero also so I would be left with
B = [1; 2; 3; 0; 5; 6; 0]
How would I do that. I think I need to use the ind2sub function but I cant get my head around how to do this
many thanks in advance :)

 Accepted Answer

B( ~any(A,2) ) = 0;

3 Comments

Hi walter! Thanks so much for such a speedy response Unfortunately this doesnt give the correct answer as it puts all the elements of the matrix as a vector so instead of B = [1; 2; 3; 0; 5; 6; 0] you get instead [1; 2; 3; 0; 0; 0; 5; 6; 0; 0; 0]
This is not right
A = [ 1 2 3; 1 2 5; 0 55 8; 0 0 0; 7 8 9; 4 0 5; 0 0 0]
B = [ 1; 2; 3; 4; 5; 6; 7]
B( ~any(A,2) ) = 0
B =
1
2
3
0
5
6
0
Ah Yes I see it now, my mistake... I input it incorrectly. Thanks for the help! :D

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!