How can you delete rows of a matrix in Matlab by some criteria?
1 answer
Let's say you have A
A = [1 2 3;4 5 0; 7 8 9; 10 NaN 12]
A =
1 2 3
4 5 0
7 8 9
10 NaN 12
Then you can select rows like this:
any(isnan(A')) ans = 0 0 0 1
To remove these NaN
-containing lines, you can do:
A(any(isnan(A')),:) = []
A =
1 2 3
4 5 0
7 8 9
You can select 0
-containing lines any(A' == 0)
. If you want all elements to be 0
or NaN
s you can use all
instead any
.
+4
source to share