Elvis Somers Edited Ytzhak Goussha Accepted Answer Beder: Array Zero Zeros Deleting Organizing Matrix

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

How to remove zeros from an array? - MATLAB Answers - MATLAB C... https://es.mathworks.com/matlabcentral/answers/330859-how-to-remove...

array zero

zeros deleting

organizing

matrix
1

Asked by Elvis Somers on 20 Mar 2017


Late st activ ity Edited by ytzhak goussha on 16 Dec 2018
Accepted Answer by Beder
3.986 views (last 30 days)

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?

Sign in to comment.

2
Link
Answer by Beder on 20 Mar 2017
Edited by MathWorks Support Team on 28 Nov 2018
Accepted Answer

This
To remove a single zero fromwebsite uses
each row of a cookies torebuild
matrix and improvetheyour user experience,
new matrix personalize
of nonzero entries, try the content and ads, and
following code: analyze website traffic. By continuing to use this website, you consent to our use of cookies.
Please see our Privacy Policy to learn more about cookies and how to change your settings.

1 of 3 09/04/2019, 21:38
How to remove zeros from an array? - MATLAB Answers - MATLAB C... https://es.mathworks.com/matlabcentral/answers/330859-how-to-remove...

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)'

Elvis Somers on 20 Mar 2017

Thanks!

Sign in to comment.

0
Link
Answer by 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?

ytzhak goussha on 16 Dec 2018

I have been working on this problme for the some time now,
so far the solution that i have reached works better if you know for certain that the number of
zeros in each row is the same, if not, then it must be converted to a cell class.

a=matrix %a big matrix with unknown number of zeroes


[sz1,sz2]=size(a);
new_sz2=zeros(1,sz1) %This array will hold the size of each row after removing zeros
for i=1:sz1
new_mat{i}= nonzeros(a(i,:));
new_row_sz(i)=size(new_mat{i},1)
end
if range(new_row_sz) == 0
new_mat=cell2mat(new_mat);
end

This website uses cookies to improve your user experience, personalize content and ads, and
analyze website traffic. By continuing to use this website, you consent to our use of cookies.
Please see our Privacy Policy to learn more about cookies and how to change your settings.
Sign in to comment.

2 of 3 09/04/2019, 21:38
How to remove zeros from an array? - MATLAB Answers - MATLAB C... https://es.mathworks.com/matlabcentral/answers/330859-how-to-remove...

Sign in to answer this question.

mathworks.com
© 1994-2019 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional
trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.

This website uses cookies to improve your user experience, personalize content and ads, and
analyze website traffic. By continuing to use this website, you consent to our use of cookies.
Please see our Privacy Policy to learn more about cookies and how to change your settings.

3 of 3 09/04/2019, 21:38

You might also like