필터 지우기
필터 지우기

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에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by