How to remove zeros from an array?
2,222 views (last 30 days)
Show older comments
Elvis Somers
on 20 Mar 2017
Edited: MathWorks Support Team
on 21 Nov 2024 at 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?
0 Comments
Accepted Answer
Beder
on 21 Nov 2024 at 0:00
Edited: MathWorks Support Team
on 21 Nov 2024 at 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 Comments
Trevon McHansen
on 23 Dec 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'.
More Answers (2)
saber kazemi
on 12 Dec 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 Comments
Stephen23
on 6 Oct 2021
Edited: Stephen23
on 6 Oct 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)
X = any(A==0,2)
A(X,:) = []
Patrick Benz
on 6 Oct 2021
Edited: Patrick Benz
on 6 Oct 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);
Mathieu PEYRE
on 2 Aug 2022
Edited: Mathieu PEYRE
on 2 Aug 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
0 Comments
See Also
Categories
Find more on Data Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!