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 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1132505/image.png)
9 commentaires
Akira Agata
le 22 Sep 2022
Modifié(e) : Akira Agata
le 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]}
Réponse acceptée
Walter Roberson
le 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 commentaire
Akira Agata
le 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)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!