I have 100x73 double mat file as shown in image. I want to remove all zero values from this file and save it to a new mat file. How can I do it? Thank you.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
sam
am 21 Sep. 2022
Kommentiert: Akira Agata
am 22 Sep. 2022
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1132505/image.png)
9 Kommentare
Akira Agata
am 22 Sep. 2022
Bearbeitet: Akira Agata
am 22 Sep. 2022
Is it OK to separate each row with different length?
Like:
A = [1 0 2;...
2 3 4]
To
C = {[1 2];...
[2 3 4]}
Akzeptierte Antwort
Walter Roberson
am 22 Sep. 2022
It is not possible to have a numeric matrix that contains blanks.
It is not possible to have a numeric matrix that contains a different number of entries in a row or column.
It is possible to write functions that display blanks in place of zeros. For example,
A = rand(5,7); A(randi(35, 1, 4)) = 0
C = compose('%6.4f', A)
C(A == 0) = {' '};
D = arrayfun(@(R) strjoin(C(R,:), ' '), (1:size(C,1)).', 'uniform', 0);
D = vertcat(D{:});
disp(D)
1 Kommentar
Akira Agata
am 22 Sep. 2022
+1
Another possible solution:
% Sample matrix
A = randi(10, 5, 7);
A(randi(35, 1, 8)) = 0
% Replace 0 with NaN and apply rmmissing for each row
A(A==0) = nan;
C = mat2cell(A, ones(size(A, 1), 1));
C = cellfun(@rmmissing, C, "UniformOutput", false)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!