Comprehension of this command: Matrix(~any(Matrix, 2), :) = [];
3 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I want to remove the zeros from a Matrix (613x30)
I have tried using this command
Matrix(Matrix==0) = [];
howoever, this command turns my matrix size from 613x30 matrix to 1x5070.
I want to maintain the 30 columns.
I found this command below that works perfectly but I don't understand what and how it does it.
Matrix(~any(Matrix, 2), :) = [];
I would appreciate it if somebody could explain me.
Thanks
2 件のコメント
the cyclist
2022 年 4 月 4 日
Suppose your input matrix is
M = [1, 2; 3, 0]
What should the output be? Remember, a numeric matrix cannot have a "hole" in it.
For some applications, replacing the zero with NaN might do what you need:
M(M==0) = NaN
採用された回答
Voss
2022 年 4 月 4 日
The function any takes a condition as its first argument and an optional second argument that says which dimension(s) the condition operates on.
The condition - the first argument - is typically an expression like x < 8 or x > -2 && y < 4 or whatever - something that returns either true or false for each element of the vector or matrix or n-dimensional array being tested. However, you can also just use a variable like you have here. Here the condition is Matrix, which is treated the same as Matrix ~= 0. That is, the condition Matrix is true when Matrix is not equal to 0 and false when Matrix is equal to 0.
The second argument, in this case 2, says to check the condition along the second dimension of Matrix. That is, along the rows, so that any(Matrix, 2) is a logical (i.e., true/false) column vector with one element per row of Matrix that says whether any element in that row of Matrix is not 0.
~ is a unary logical operator called NOT that returns the logical negation of the input, so that ~true is false and ~false is true. Thus, ~any(Matrix, 2) is a logical column vector that is true wherever a row of Matrix does not have any non-zero elements and false otherwise. Put another way, ~any(Matrix, 2) is true for rows of Matrix that are all 0 and false for rows that are not all 0.
Matrix(idx,:) = []; removes the rows specified by idx from the matrix Matrix. idx could be positive integer indices, e.g., [1 3 4] to remove rows 1, 3, and 4 from Matrix, or logical indices, e.g., [true false true true] to do the same thing. (Using logicals as indices into an array is called logical indexing and is a very powerful feature of MATLAB; read more about it here and here.)
In this case the command is like Matrix(idx,:) = []; except idx itself is derived from the logic involving any explained above. So the effect is to remove all rows from Matrix that are all zeros.
Some related examples to illustrate:
Matrix = eye(5); % 5-by-5 identity matrix
Matrix(4,4) = 0 % make the 4th row all zeros
Matrix(~any(Matrix,2),:) = [] % remove any all-zero rows
% More examples:
Matrix = magic(5)
any(Matrix,2) % any non-zero element in each row? yes, of course
any(Matrix == 12,2) % any 12s in each row? only one row has a 12
any(Matrix == 12,1) % any 12s in each column? only one column has a 12
idx = any(Matrix > 22,2) % any element greater than 22 in each row? the 1st, 2nd and 5th row
Matrix(idx,:) = [] % delete those rows with an element > 22, leaving just the 3rd and 4th rows
Matrix = magic(5); % restore Matrix
Matrix(any(Matrix > 22,2),:) = [] % same thing as before, but in one line
Matrix = magic(5); % restore Matrix
Matrix(~any(Matrix > 22,2),:) = [] % delete rows without any element > 22, leaves rows 1, 2, and 5
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!