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

1 view (last 30 days)
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

Walter Roberson
Walter Roberson on 6 Mar 2013
B( ~any(A,2) ) = 0;
  3 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 6 Mar 2013
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

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!