0
votes

I have a question and I hope it is not duplicate.

First, I have let say the following matrix:

A=[2 2 2 0 0
   1 2 3 0 0
   4 5 7 2 0]

I want to remove the zeros from A and return:

A=[2 2 2
   1 2 3
   4 5 7]

When I do

A(A==0)=[] 

I get

A=[2 2 2 1 2 3 4 5 7]

Second, if instead of zeros I want to remove the elements that are greater than something. For example if I want to remove all elements greater than 6 (>6) of the following matrix B:

B=[2 2 2 5 3
   1 2 3 6 8
   4 5 7 2 1]

I get

A=[2 2 5
   1 2 6
   4 5 2]

P.S. I know how to do it using loops.

1
A(:,find(all(A,1)))Divakar
And what should happen with the 2 in the lower right corner?Robert Seifert
@thewaywewalk I remove all the column whenever I find in it a 0.npisinp
For second problem - B(:,~any(B>6,1))Divakar
@Floris, Done. I was expecting something better as my 100th answer :) Oh wellDivakar

1 Answers

1
votes

First problem solution

A(:,find(all(A,1)))

Second problem solution

B(:,~any(B>6,1))