Extract first non-NaN of each row without loop
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dave
el 24 de Oct. de 2021
Comentada: Dave
el 24 de Oct. de 2021
Hello, from a matrix where each row contains at least one non-NaN, is there away to extract the FIRST non-nan of each row without using a loop?
A=[NaN,444,NaN,NaN,41;
NaN,NaN,NaN,222,NaN;
NaN,NaN,554,554,NaN;
125,747,21,NaN,4;
NaN,NaN,NaN,43,NaN;
NaN,98,NaN,75,NaN;
NaN,22,NaN,NaN,NaN]
From matrix A, I would like to arrive to B
B=[444;222;554;125;43;98;22]
Thanks
1 comentario
Respuesta aceptada
Walter Roberson
el 24 de Oct. de 2021
A=[NaN,444,NaN,NaN,41;
NaN,NaN,NaN,222,NaN;
NaN,NaN,554,554,NaN;
125,747,21,NaN,4;
NaN,NaN,NaN,43,NaN;
NaN,98,NaN,75,NaN;
NaN,22,NaN,NaN,NaN]
B = A(sub2ind(size(A), (1:size(A,1)).', sum(cumprod(isnan(A),2),2)+1))
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!