フィルターのクリア

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

1 回表示 (過去 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
  1 件のコメント
MP
MP 2022 年 7 月 28 日
Thank you so much that helped @Chunru

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by