How to make a number NaN when its both neighbours are NaN?

조회 수: 3 (최근 30일)
MP
MP 2022년 7월 28일
댓글: MP 2022년 7월 28일
I would like to find the numbers sandwiched between two NaN's and make them NaN.
I have a matrix:
A = [NaN 2.35 NaN 2.358 1.68 1.98 2.88 NaN 2.68 NaN 2.70 NaN 2.20 NaN 3.03; NaN NaN 2.77 NaN 2.67 1.87 2.56 1.88 2.39 NaN 2.77 2.83 NaN 2.59 NaN];
I would like to check the numbers that is sourrounded by NaN values on both the sides.
If the number has NaN on both sides, then make that number as NaN;
I want the output to be like,
B = = [NaN NaN NaN 2.358 1.68 1.98 2.88 NaN NaN NaN NaN NaN NaN NaN 3.03; NaN NaN NaN NaN 2.67 1.87 2.56 1.88 2.39 NaN 2.77 2.83 NaN NaN NaN];

채택된 답변

Chunru
Chunru 2022년 7월 28일
A = [NaN 2.35 NaN 2.358 1.68 1.98 2.88 NaN 2.68 NaN 2.70 NaN 2.20 NaN 3.03;
NaN NaN 2.77 NaN 2.67 1.87 2.56 1.88 2.39 NaN 2.77 2.83 NaN 2.59 NaN];
B = [NaN NaN NaN 2.358 1.68 1.98 2.88 NaN NaN NaN NaN NaN NaN NaN 3.03;
NaN NaN NaN NaN 2.67 1.87 2.56 1.88 2.39 NaN 2.77 2.83 NaN NaN NaN];
C = A;
for i=1:size(C, 1)
for j=2:size(C,2)-1
if isnan(C(i, j-1)) && isnan(C(i, j+1))
C(i, j) = NaN;
end
end
end
A, B, C
A = 2×15
NaN 2.3500 NaN 2.3580 1.6800 1.9800 2.8800 NaN 2.6800 NaN 2.7000 NaN 2.2000 NaN 3.0300 NaN NaN 2.7700 NaN 2.6700 1.8700 2.5600 1.8800 2.3900 NaN 2.7700 2.8300 NaN 2.5900 NaN
B = 2×15
NaN NaN NaN 2.3580 1.6800 1.9800 2.8800 NaN NaN NaN NaN NaN NaN NaN 3.0300 NaN NaN NaN NaN 2.6700 1.8700 2.5600 1.8800 2.3900 NaN 2.7700 2.8300 NaN NaN NaN
C = 2×15
NaN NaN NaN 2.3580 1.6800 1.9800 2.8800 NaN NaN NaN NaN NaN NaN NaN 3.0300 NaN NaN NaN NaN 2.6700 1.8700 2.5600 1.8800 2.3900 NaN 2.7700 2.8300 NaN NaN NaN

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by