Removing zero rows from a matrix

In summary, the conversation discusses how to remove rows of a matrix that contain only zeros in MATLAB. The suggested solution is to use the syntax a(all(a==0,2),:)=[] which checks for rows where all elements are equal to zero and removes them from the matrix. The conversation also explores alternative methods and explains why a "for" loop cannot be used in this situation.
  • #1
mikeph
1,235
18
say,

Code:
a = [1,2,3; 0,0,0; 0,4,5];

how can I remove the rows of a that contain only zeros?

I found a link which works, but I don't know why:
http://www.mathworks.in/matlabcentral/newsreader/view_thread/281578
Suggesting:
Code:
a(all(a==0,2),:)=[]

1. what does the "=[]" syntax do
2. can this method be used to generate a second matrix b = a(without any zero rows)? I tried re-writing in this form and it doesn't work:

Code:
>> b = a(all(a==0,2),:)
b =
     0     0     0

Clearly I'm missing something,
Thanks
 
Physics news on Phys.org
  • #2
If you want something that's a bit less cryptic and a bit more transparent you could do it like this

Code:
k=0; rows=size(a)(1)
while (k<rows)
   k=k+1;
   if all(a(k,:)==0)
      a(k,:)=[];
      rows=rows-1;
   end
end

Note that you can't use a "for" loop because if a row is removed then it will reduce the size of the matrix, causing the for loop index to eventually exceed the size of the matrix. This problem of course only arises because you're processing the matrix "in place".

BTW, [] just represents an empty vector.
 
Last edited:

What does it mean to remove zero rows from a matrix?

Removing zero rows from a matrix means to delete any rows in the matrix that contain all zeroes. This can be done to simplify the matrix and make it more efficient to work with.

Why would you want to remove zero rows from a matrix?

Removing zero rows from a matrix can help streamline calculations and make the matrix easier to read and interpret. It can also help reduce the size of the matrix, making it more manageable in terms of memory and processing power.

How do you remove zero rows from a matrix?

To remove zero rows from a matrix, you can use a combination of matrix operations such as row deletion, filtering, or masking. These operations can be performed using mathematical software or programming languages like MATLAB or Python.

Can you remove zero rows from a matrix without changing the original matrix?

Yes, it is possible to remove zero rows from a matrix without altering the original matrix. This can be done by creating a copy of the original matrix and performing the removal operations on the copy instead of the original.

What are the potential implications of removing zero rows from a matrix?

Removing zero rows from a matrix can affect the final results of any calculations or analyses performed on the matrix. It is important to carefully consider the implications and potential consequences before removing zero rows from a matrix.

Similar threads

  • Precalculus Mathematics Homework Help
Replies
1
Views
527
  • Precalculus Mathematics Homework Help
Replies
32
Views
838
  • Precalculus Mathematics Homework Help
Replies
1
Views
724
  • Linear and Abstract Algebra
Replies
3
Views
2K
  • Linear and Abstract Algebra
Replies
1
Views
805
  • Linear and Abstract Algebra
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
14
Views
1K
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
Back
Top