How to remove zeros from an array?
1,645 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Beder
2017-3-20
编辑:MathWorks Support Team
2018-11-28
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 个评论
Trevon McHansen
2021-12-23
@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'.
更多回答(2 个)
saber kazemi
2018-12-12
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 个评论
Stephen23
2021-10-6
编辑:Stephen23
2021-10-6
"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
2021-10-6
编辑:Patrick Benz
2021-10-6
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
2022-8-2
编辑:Mathieu PEYRE
2022-8-2
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!