# How to delete a zeros in matrix?

172 views (last 30 days)
Arul prasath on 7 Feb 2015
Commented: Arul prasath on 8 Feb 2015
I have a matrix like this
a=[ 0.7446 0.0364 186.1153 0 134.5022 114.8212 84.7745 130.8661
0 0 13.8840 0 16.1683 10.4461 69.8035 114.2774];
i want to delete zeros and their corresponding values. The solution should be like this...
b= [186.1153 134.5022 114.8212 84.7745 130.8661
13.8840 16.1683 10.4461 69.8035 114.2774];

Image Analyst on 7 Feb 2015
This will get rid of all columns that have a zero anywhere in them:
colsWithZeros = any(a==0)
b = a(:, ~colsWithZeros)
##### 2 CommentsShowHide 1 older comment
Arul prasath on 8 Feb 2015
Image Analyst- Thank you

Jason Moore on 7 Feb 2015
Edited: Jason Moore on 7 Feb 2015
I think this question was already asked before but this code should do it.
b = a(find(a~=0))
##### 2 CommentsShowHide 1 older comment
Arul prasath on 8 Feb 2015
Thank you

ulgi onor on 7 Feb 2015
a(a==0)=[]
a =
Columns 1 through 9 0.7446 0.0364 186.1153 13.8840 134.5022 16.1683 114.8212 10.4461 84.7745 Columns 10 through 12 69.8035 130.8661 114.2774
##### 2 CommentsShowHide 1 older comment
David Young on 7 Feb 2015
Also note that your result has different elements to the matrix b in the question.