Azzera filtri
Azzera filtri

Comprehension of this command: Matrix(~any(Matrix, 2), :) = [];

9 visualizzazioni (ultimi 30 giorni)
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 Commenti
the cyclist
the cyclist il 4 Apr 2022
Suppose your input matrix is
M = [1, 2; 3, 0]
M = 2×2
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
M = 2×2
1 2 3 NaN
Bob
Bob il 6 Apr 2022
Thank you, both for your answers.

Accedi per commentare.

Risposta accettata

Voss
Voss il 4 Apr 2022
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 = 5×5
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
Matrix(~any(Matrix,2),:) = [] % remove any all-zero rows
Matrix = 4×5
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1
% More examples:
Matrix = magic(5)
Matrix = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
any(Matrix,2) % any non-zero element in each row? yes, of course
ans = 5×1 logical array
1 1 1 1 1
any(Matrix == 12,2) % any 12s in each row? only one row has a 12
ans = 5×1 logical array
0 0 0 1 0
any(Matrix == 12,1) % any 12s in each column? only one column has a 12
ans = 1×5 logical array
0 1 0 0 0
idx = any(Matrix > 22,2) % any element greater than 22 in each row? the 1st, 2nd and 5th row
idx = 5×1 logical array
1 1 0 0 1
Matrix(idx,:) = [] % delete those rows with an element > 22, leaving just the 3rd and 4th rows
Matrix = 2×5
4 6 13 20 22 10 12 19 21 3
Matrix = magic(5); % restore Matrix
Matrix(any(Matrix > 22,2),:) = [] % same thing as before, but in one line
Matrix = 2×5
4 6 13 20 22 10 12 19 21 3
Matrix = magic(5); % restore Matrix
Matrix(~any(Matrix > 22,2),:) = [] % delete rows without any element > 22, leaves rows 1, 2, and 5
Matrix = 3×5
17 24 1 8 15 23 5 7 14 16 11 18 25 2 9

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by