How to remove zeros from an array?

2 712 views (last 30 days)
Elvis Somers
Elvis Somers on 20 Mar 2017
Edited: Mathieu PEYRE on 2 Aug 2022
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?

Accepted Answer

Beder on 20 Mar 2017
Edited: MathWorks Support Team on 28 Nov 2018
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)'
Trevon McHansen
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'.

Sign in to comment.

More Answers (2)

saber kazemi
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?
Patrick Benz
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?
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
[LIA, LOCB]=ismember(Elementset_Nodes(:,2:end),Node_Coord(:,1));
for i=1:length(LOCB)
if sum(X)~=0
for j=1:length(X)
[r, c]=find(new_Mat);

Sign in to comment.

Mathieu PEYRE
Mathieu PEYRE on 2 Aug 2022
Edited: Mathieu PEYRE on 2 Aug 2022
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)
for i=1:length(A)
if A(i,1)~=0 || A(i,2)~=0 || A(i,3)~=0


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!