Wednesday, January 16, 2013

removing a vector [0 0 0] from a matrix M

Imagine a matrix M having some [0 0 0] vector like the following which I want to remove 

>> M=vertcat([0 0 0],magic(3),[0 0 0])

M =

     0     0     0
     8     1     6
     3     5     7
     4     9     2
     0     0     0
The easiest way to do it is the following 
Step 1. find the logical row positions which have [0 0 0] entries by where all(M==0,2) means looking for zero entries in row dimension


>> zero_position=all(M==0,2)

zero_position =

     1
     0
     0
     0
     1

Step2: Clean the zero entry rows!


>> M(zero_position , : )=[]

M =

     8     1     6
     3     5     7
     4     9     2


No comments:

Post a Comment

Please ask if anything is not clear enough..........