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 view (last 30 days)
Show older comments
9 Comments
Akira Agata
on 22 Sep 2022
Edited: Akira Agata
on 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]}
Accepted Answer
Walter Roberson
on 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 Comment
Akira Agata
on 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)
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!