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 次查看(过去 30 天)
  9 个评论
Akira Agata
Akira Agata 2022-9-22
编辑:Akira Agata 2022-9-22
Is it OK to separate each row with different length?
Like:
A = [1 0 2;...
2 3 4]
A = 2×3
1 0 2 2 3 4
To
C = {[1 2];...
[2 3 4]}
C = 2×1 cell array
{[ 1 2]} {[2 3 4]}

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-9-22
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
A = 5×7
0.2379 0.2298 0.7207 0.1782 0 0.7270 0.1074 0.7013 0.7682 0.7379 0.6983 0.1428 0.6798 0 0.3510 0 0.6469 0.2545 0.8296 0.5563 0.3290 0.2298 0.5079 0.0852 0.4941 0.9429 0.1410 0.2596 0.2925 0.0965 0.9475 0.6772 0 0.5899 0.3218
C = compose('%6.4f', A)
C = 5×7 cell array
{'0.2379'} {'0.2298'} {'0.7207'} {'0.1782'} {'0.0000'} {'0.7270'} {'0.1074'} {'0.7013'} {'0.7682'} {'0.7379'} {'0.6983'} {'0.1428'} {'0.6798'} {'0.0000'} {'0.3510'} {'0.0000'} {'0.6469'} {'0.2545'} {'0.8296'} {'0.5563'} {'0.3290'} {'0.2298'} {'0.5079'} {'0.0852'} {'0.4941'} {'0.9429'} {'0.1410'} {'0.2596'} {'0.2925'} {'0.0965'} {'0.9475'} {'0.6772'} {'0.0000'} {'0.5899'} {'0.3218'}
C(A == 0) = {' '};
D = arrayfun(@(R) strjoin(C(R,:), ' '), (1:size(C,1)).', 'uniform', 0);
D = vertcat(D{:});
disp(D)
0.2379 0.2298 0.7207 0.1782 0.7270 0.1074 0.7013 0.7682 0.7379 0.6983 0.1428 0.6798 0.3510 0.6469 0.2545 0.8296 0.5563 0.3290 0.2298 0.5079 0.0852 0.4941 0.9429 0.1410 0.2596 0.2925 0.0965 0.9475 0.6772 0.5899 0.3218
  1 个评论
Akira Agata
Akira Agata 2022-9-22
+1
Another possible solution:
% Sample matrix
A = randi(10, 5, 7);
A(randi(35, 1, 8)) = 0
A = 5×7
8 0 8 9 3 6 5 10 2 1 6 3 6 10 0 0 0 6 1 10 2 0 3 4 10 6 0 0 10 10 6 6 6 5 1
% 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)
C = 5×1 cell array
{[ 8 8 9 3 6 5]} {[10 2 1 6 3 6 10]} {[ 6 1 10 2]} {[ 3 4 10 6]} {[10 10 6 6 6 5 1]}

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by