zebda
zebda

Reputation: 95

Remove rows meeting specific condition

I have a matrix in MATLAB as follows:

A = [ 0, 0, 0;
      1, 2, 3;
      4, 2, 3;
      0, 0, 0;
      1, 3, 4 ];

I would like to remove the zeros from line 1 and line 4 so I can get the following matrix:

B = [ 1, 2, 3;
      4, 2, 3;
      1, 3, 4 ];

What I did is the following:

for i = 1:length(A)
    if (sum(A(i, :)) == 0)
        A(i, :) = [];
    end
end

I know that the problem is that A is changing in the size so that is the problem. Unfortunately, I cannot fix it. Could you please give me some hint.

Upvotes: 1

Views: 80

Answers (2)

rayryeng
rayryeng

Reputation: 104514

Kamtal's approach would work assuming that you have no rows that summed to zero. To be safer, you should check to see if a row consists only of zeroes. In other words, try doing this:

A(~any(A,2), :) = [];

any checks to see if an array or matrix has any of its elements to be non-zero which outputs logical true and false otherwise. If you use any over a matrix, you can specify whether you want to check for this over the rows or the columns in the second parameter of any. In your case, you want to operate over the columns for each row, and so you'd specify the second parameter to be 2.

Because you want to check to see if any row consists of all zeroes, you would use any over the columns and check for the reverse. As such, any would output logical false if all of a row consisted only of zeroes. This is what we're looking for, but you want this to be true and not false, and hence we invert using ~ as the opposite is what we seek.


However, if you want to get your method working, you would not use a for loop, but a while loop instead as the index to access each row keeps changing. As such, once you remove a row, you would not increment the index to access the next row, until you successfully find a row which doesn't sum to zero. Remember, as you remove a row, the row that was just below it gets moved to the position that was removed, and so you would still check this same position for a row that didn't sum to zero. You would move to the next row once you find a row that didn't sum to zero.

In other words, do this:

i = 1;
while i < length(A)
    if (sum(A(i, :)) == 0)
        A(i, :) = [];
        continue;
    end
    i = i + 1;
end

Upvotes: 3

Rash
Rash

Reputation: 4336

If you want to remove the rows with zero sum this might help,

A(sum(A,2)==0,:)=[];

As mentioned by @LuisMendo you can use

A(all(A,2)==0,:)=[];

depending on what criteria you have in mind.

Upvotes: 5

Related Questions