0
votes

I have a matrix whose size is 4x16 and I need to remove all columns which have the value of first row is zero. For example: here is the matrix example :

X = [1 0 0 0; 0 1 0 0; 0 0.4 0 0; 0 0 0 0.8; 0.5 0 0 0];

The 2D matrix (X) looks like this:

      1   0   0   0
      0   1   0   0
      0  0.4  0   0
      0   0   0  0.8
     0.5  0   0   0

So what I need is just X = [1 0 0 0;0.5 0 0 0 ] because the first row of these columns are different of Zero.

1

1 Answers

3
votes

Just exclude that row numbers:

X = X(X(:,1) ~= 0, :);

X(:,1) ~= 0 is a boolean vector for the first column which is true if the value of the element would not be 0.