MATLAB: How to delete a zeros in matrix

mathematicsmatrixmatrix arraymatrix manipulation

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];

Best Answer

This will get rid of all columns that have a zero anywhere in them:
colsWithZeros = any(a==0)
b = a(:, ~colsWithZeros)
Related Question