How to make a number NaN when its both neighbours are NaN?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Chunru
el 28 de Jul. de 2022
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
Más respuestas (0)
Ver también
Categorías
Más información sobre NaNs en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!