How to make a number NaN when its both neighbours are NaN?
1 次查看(过去 30 天)
显示 更早的评论
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];
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!