How to replace the numbers in matrix?

조회 수: 5 (최근 30일)
Han-Yun Chiang
Han-Yun Chiang 2022년 1월 17일
댓글: Voss 2022년 1월 18일
Hi,
I'm trying to replace the zeros to the average numbers in between before and after numbers, how to do it?
[1,2,3,0,5,6,0,8,9 ; 1,3,0,0,0,11,13,15,17]
become
[1,2,3,4,5,6,7,8,9 ; 1,3,5,7,9,11,13,15,17]
Thank you
  댓글 수: 2
Adam Danz
Adam Danz 2022년 1월 18일
In the updated question, it's not clear how the first two rows below become the last two rows. It looks like you're taking the average in some cases but it's not clear what happens when there are consecutive 0s. Please explain the conversion rule(s).
1 2 3 0 5 6 0 8 9
1 3 0 0 0 10 12 14 16
1 2 3 4 5 6 7 8 9
1 3 5 7 9 10 12 14 16
Han-Yun Chiang
Han-Yun Chiang 2022년 1월 18일
I am so sorry. The second row should be 1,3,5,7,9,11,13,15,17.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 1월 18일
Here is a way to do it using linear interpolation on each row. It will interpolate between the non-zero numbers to fill in the zeros (at least two non-zero numbers are required on a given row in order to interpolate) and leave zeros at the beginning or end of the row unchanged. Demonstrated with a slightly more general case:
A = [1,2,3,0,5,6,0,8,9 ; 1,3,0,0,0,11,13,15,17; 0 0 0 0 0 0 0 0 0; 0 0 3 0 0 7 0 0 0]
A = 4×9
1 2 3 0 5 6 0 8 9 1 3 0 0 0 11 13 15 17 0 0 0 0 0 0 0 0 0 0 0 3 0 0 7 0 0 0
c = 1:size(A,2);
for i = 1:size(A,1)
idx = A(i,:) == 0;
if nnz(~idx) < 2
continue
end
A(i,idx) = interp1(c(~idx),A(i,~idx),c(idx),'linear',0);
end
display(A);
A = 4×9
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 1.0000 3.0000 5.0000 7.0000 9.0000 11.0000 13.0000 15.0000 17.0000 0 0 0 0 0 0 0 0 0 0 0 3.0000 4.3333 5.6667 7.0000 0 0 0
  댓글 수: 4
Han-Yun Chiang
Han-Yun Chiang 2022년 1월 18일
Done!
It works when I typed in the data in the script, however, I couldn't apply it to the import data (numeric matrix form). Do you know how to amened it ?
Voss
Voss 2022년 1월 18일
@Han-Yun Chiang Sorry, I never use "Import Data", so I don't know if there's a way to apply this code when importing data from a file. You may have to just import the data and then run this code on it afterwards.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2022년 1월 17일
>I'm trying to replace the zeros to the intermediate numbers
I assume you mean you want to replace 0s with the value immediatly above or below the 0. If the entire columns contains 0s, nothing will change.
A = [1,2,3,0,5,6,0,8,9 ; 1,3,0,0,0,10,12,14,16]
A = 2×9
1 2 3 0 5 6 0 8 9 1 3 0 0 0 10 12 14 16
A(A==0) = A(flipud(A)==0)
A = 2×9
1 2 3 0 5 6 12 8 9 1 3 3 0 5 10 12 14 16
  댓글 수: 1
Han-Yun Chiang
Han-Yun Chiang 2022년 1월 18일
Thank you for your reply.
I made an amendment to express my question more clearly. Please have a look

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by