How to merge adjacent NaN values into single NaN value in a vector?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have [1 2 3 NaN NaN 0 1 2 NaN 9 8 7 6 NaN NaN NaN 1 1] anf I want [1 2 3 NaN 0 1 2 NaN 9 8 7 6 NaN 1 1] without a for cycle. Are there any trick to do this?
1 commentaire
Stephen23
le 1 Juil 2018
@Mr M: please remember to accept answers or provide feedback on your earlier questions:
Réponse acceptée
Stephen23
le 1 Juil 2018
Modifié(e) : Stephen23
le 3 Juil 2018
No loops required, no tricks required, just basic MATLAB indexing:
old = [1 2 3 NaN NaN 0 1 2 NaN 9 8 7 6 NaN NaN NaN 1 1];
idx = ~isnan(old);
new = old(idx | [true,idx(1:end-1)]);
3 commentaires
Stephen23
le 3 Juil 2018
Modifié(e) : Stephen23
le 3 Juil 2018
@Mr M.: you are right. It is easy to fix, just exchange the false for true:
>> old = [NaN,NaN,NaN,1,2,3,NaN,NaN,0,1,2,NaN,9,8,7,6,NaN,NaN,NaN]
old =
NaN NaN NaN 1 2 3 NaN NaN 0 1 2 NaN 9 8 7 6 NaN NaN NaN
>> idx = ~isnan(old);
>> new = old(idx | [true,idx(1:end-1)])
new =
NaN 1 2 3 NaN 0 1 2 NaN 9 8 7 6 NaN
Plus de réponses (1)
Andrei Bobrov
le 2 Juil 2018
a = [1 2 3 NaN NaN 0 1 2 NaN 9 8 7 6 NaN NaN NaN 1 1];
lo = ~isnan(a);
lo(strfind(lo,[0 1])) = true;
out = a(lo);
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!