How can I eliminate unwanted zeros from a matrix?
Ältere Kommentare anzeigen
e.g I want to make this:
1,1,1,1,0,0
1,1,0,0,0,0
1,0,0,0,0,0
1,1,1,1,1,0
into this:
1,1,1,1
1,1
1
1,1,1,1,1
...
Thanks
3 Kommentare
Catherine
am 16 Jun. 2016
This is exactly what I want to do as well! I want to be able to remove all the zeros yet in a matrix form.
I have a matrix:
A =
557 577 598 0 0 0 0 0 0 0
0 0 0 558 578 599 0 0 0 0
0 0 0 0 0 0 558 579 0 0
0 0 0 0 0 0 0 0 559 579
and I want it to be
A =
557 577 598
558 578 599
NaN 558 579
NaN 559 579
Guillaume
am 16 Jun. 2016
Catherine, please start your own question rather than adding to somebody's else.
Roger Stafford
am 17 Jun. 2016
@Catherine: Let A be your original matrix. You can accomplish what you want with:
T = A~=0;
n = sum(T,2);
m = max(n);
B = nan(size(A,1),m);
for k = 1:size(A,1)
B(k,m-n(k)+1:m) = A(k,T(k,:)); % <-- B is the result
end
Antworten (2)
Justin
am 24 Apr. 2014
Bearbeitet: per isakson
am 17 Jun. 2016
It depends on what you are trying to end up with exactly but a good approach would be to set all zero values to NaN. NaNs wont show up in plots and you can use nanmean() or other functions which ignore NaNs to work with your matrix. This will also retain the shape of your matrix.
arr = [1 1 0; 1 0 1; 0 0 1];
arr =
1 1 0
1 0 1
0 0 1
arr(arr==0) = nan
arr =
1 1 NaN
1 NaN 1
NaN NaN 1
Another option is to use a sparse matrix where the memory only retains information about the index of values.
sparse(arr)
ans =
(1,1) 1
(2,1) 1
(1,2) 1
(2,3) 1
(3,3) 1
You could even convert the array into a cell array and use cellfun to replace all the zeros with an empty array. It would be much more difficult to operate on the information then though.
What are you trying to accomplish with the data?
1 Kommentar
Mohamad Mossad
am 16 Dez. 2020
Thanks, setting up zeros to nan is a great idea. 6 years later.
per isakson
am 24 Apr. 2014
Hint:
>> num = [ 1,1,1,1,0,0 ];
>> num( num == 0 ) = []
num =
1 1 1 1
6 Kommentare
Justin
am 24 Apr. 2014
This might not work if there is more than one row of data. For instance:
arr = [1 1 0; 1 0 1; 0 0 1]
arr =
1 1 0
1 0 1
0 0 1
arr(arr == 0) = []
arr =
1 1 1 1 1
per isakson
am 24 Apr. 2014
Certainly it will not work with a 2D matrix. However, why should I guess what type of output Mark want for 2D matrices.
Justin
am 24 Apr. 2014
Well I assumed he would want a 2D results given the multiple rows shown in his question description. Of course it may not be so but it is important to understand this limitation.
per isakson
am 24 Apr. 2014
The correct answer is that for 2D it is not possible. That's because the result will not be a matrix. Why not let Mark find that out himself and return with a clarification.
Image Analyst
am 24 Apr. 2014
Or the output could be a cell array where you can have different sized rows in each cell. But I don't know why you'd want to mess with that or even want what he's asking for in the first place. Chances are he won't need what he asked for once we learn what he plans on doing with the result.
per isakson
am 24 Apr. 2014
Bearbeitet: per isakson
am 24 Apr. 2014
Agree! I know, I should not try to answer questions like this one.
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!