How to remove zeros from an array?

2.138 visualizaciones (últimos 30 días)
Elvis Somers
Elvis Somers el 20 de Mzo. de 2017
Editada: MathWorks Support Team el 21 de Nov. de 2024 a las 6:22
I want to remove zeroes from an array. The array has exactly one zero per row. For example: a = [1 4 0 3; 0 1 5 5; 1 0 8 1; 5 4 4 0; 0 1 5 2] Should be turned into a = [1 4 3; 1 5 5; 1 8 1; 5 4 4; 1 5 2] I have tried using the command a(a==0) = []; However, this turns the 2000x50 array into an 1x98000 array instead of an 2000x49 array like I want it. Any ideas?

Respuesta aceptada

Beder
Beder el 21 de Nov. de 2024 a las 0:00
Editada: MathWorks Support Team el 21 de Nov. de 2024 a las 6:22
To remove a single zero from each row of a matrix and rebuild the new matrix of nonzero entries, try the following code: a = [1 4 0 3; 0 1 5 5; 1 0 8 1; 5 4 4 0; 0 1 5 2] v = nonzeros(a'); newmat = reshape(v,3,5)'
  3 comentarios
eloy garcia venegas
eloy garcia venegas el 26 de Nov. de 2021
why a' and not just a?
Trevon McHansen
Trevon McHansen el 23 de Dic. de 2021
@eloy garcia venegas If you give it a try in MATLAB you'll see that getting the appropriate sized output takes a bit of thinking.
Calling nonzeros on the matrix a will return a vector of elements. This is because MATLAB doesn't attempt to "naturally" resize the outputs any other way. (because MATLAB doesn't actually know how many zeros it will omit or what their relationships are to one another).
Since we know that there are exactly 1 '0' elements per row, we know that the output matrix size will be the size of the input matrix with one less column. So the size will go from 5,4 to 5,3.
When you get the vector from nonzeros, the values are considered column-by-column. But we're considering a row-wise size adjustment.
By transposing a in the nonzeros call, you're effectively telling it to treat rows as columns and vice versa, making it behave "as if it were a row-wise operation".
Then when you get your row-wise output, you can reshape to a matrix. Reshape is also a column-wise operation, it will take the vector v, and assign values column-first. So to get around this, Beder first reshaped directly into a 3,5 and then transposed the entire matrix.
To get a more visual take on this. Try to get the right output without using a'.

Iniciar sesión para comentar.

Más respuestas (2)

saber kazemi
saber kazemi el 12 de Dic. de 2018
If we do not know how much of the elements to submit after we remove the zero elements.
a = [is a big matrix]
v = nonzeros(a');
newmat = reshape(v,?,?)'
Any ideas?
  6 comentarios
Stephen23
Stephen23 el 6 de Oct. de 2021
Editada: Stephen23 el 6 de Oct. de 2021
"Both methods don't seem to work for me."
Neither method is suitable for your data. Both methods are fragile and should be avoided.
The simplest approach is to detect the values that you want to remove and then use ANY with its dimension argument (or whatever logic you need for your task) to create a logical index vector of the rows that you need to remove. Then use that logical index to remove the rows.
For example:
A = randi([0,3],7,4)
A = 7×4
2 1 1 3 2 0 0 0 3 0 3 0 1 2 0 0 3 0 0 1 3 3 2 1 0 0 0 0
X = any(A==0,2)
X = 7×1 logical array
0 1 1 1 1 0 1
A(X,:) = []
A = 2×4
2 1 1 3 3 3 2 1
Patrick Benz
Patrick Benz el 6 de Oct. de 2021
Editada: Patrick Benz el 6 de Oct. de 2021
Das scheint zumindest schon einmal für die Reihen zu funktionieren, welche nur 0 Elemente enthalten.
Bei den gemischten Reihen funktioniert es aber offensichtlich nicht, da nur die Reihen von A behalten werden, welche keine 0 enthalten.
Wenn ich den Code so auf meine Matrix anwende, bekomme ich einen 0 x 20 double als Ergebnis.
Wäre es einfacher, wenn ich wüsste, wie viele 0 pro Reihe in der Matrix enthalten sind?
Edit:
Auch wenn es sicherlich nicht das schnellste oder eleganteste Ergebnis ist, habe ich es mit geschachtelten Schleifen geschafft.
Elementset_Nodes = 22762 x 21 double
Node_Coord = 7765 x 1 double
r=1;
[LIA, LOCB]=ismember(Elementset_Nodes(:,2:end),Node_Coord(:,1));
for i=1:length(LOCB)
X=0;
X=find(LOCB(i,:));
if sum(X)~=0
for j=1:length(X)
new_Mat(r,1)=Elementset_Nodes(i,1);
new_Mat(r,j+1)=LOCB(i,X(j));
end
else
new_Mat(r,:)=0;
new_Mat(r,:)=[];
end
[r, c]=find(new_Mat);
r=max(r)+1;
end
Elementset_Nodes=horzcat(Elementset_Nodes(:,1),new_Mat);

Iniciar sesión para comentar.


Mathieu PEYRE
Mathieu PEYRE el 2 de Ag. de 2022
Editada: Mathieu PEYRE el 2 de Ag. de 2022
Hello,
Rather than remove the zeros of your matrix, you can create an other matrix with the non zeros rows in it.
EXEMPLE: A=[ 1 2 3; 0 0 0; 3 4 5; 0 1 0] and you want B=[1 2 3; 3 4 5; 0 1 0]
(If you want to remove all rows with at least one zero and obtain B=[1 2 3; 3 4 5], you can replace the "||" by "&&" in the "if" condition)
L=1;
for i=1:length(A)
if A(i,1)~=0 || A(i,2)~=0 || A(i,3)~=0
B(L,:)=A(i,:);
L=L+1;
end
end

Categorías

Más información sobre Data Types en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by