Shift data in row when NaNs occurs

3 visualizaciones (últimos 30 días)
Márcia Vicente
Márcia Vicente el 22 de Ag. de 2019
Comentada: Márcia Vicente el 22 de Ag. de 2019
I have a Cell array 15x112 that contains NaNs.
I want to shift the data in the rows, everytime I get a NaN.
% I have for instance:
0.30 NaN 0.24 0.26 0.67 0.37
0.49 0.35 0.55 NaN 0.43 0.54 . . .
% and I want to get:
0.30 0.24 0.26 0.67 0.37
0.49 0.35 0.55 0.43 0.54 . . .
Is there an easy way to do it?
Thanks

Respuesta aceptada

Stephen23
Stephen23 el 22 de Ag. de 2019
>> M = [0.30,NaN,0.24,0.26,0.67,0.37;0.49,0.35,0.55,NaN,0.43,0.54]
M =
0.30000 NaN 0.24000 0.26000 0.67000 0.37000
0.49000 0.35000 0.55000 NaN 0.43000 0.54000
>> [~,X] = sort(isnan(M),2);
>> for k = 1:size(M,1), M(k,:) = M(k,X(k,:)); end
>> M
M =
0.30000 0.24000 0.26000 0.67000 0.37000 NaN
0.49000 0.35000 0.55000 0.43000 0.54000 NaN

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays 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!

Translated by