How to combine arrays using NaN?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sean O'Donnell
le 12 Déc 2018
Commenté : Sean O'Donnell
le 13 Déc 2018
I have two arrays, one longer than the other. The long array is sequential from 1 to some value, with NaN filling some elements instead of the value of that element. The length of the short array is the length of the long array without the NaN elements. How do I combine the arrays so that the new array will be the same length as the long array, and will have NaN in the same elements as in the long array, and the data from the short array in the rest of the elements?
Ex:
A = [1 2 3 NaN 5 6 NaN 8 NaN 10];
B = [96 12 83 71 34 64 21];
FinalArray = [96 12 83 NaN 71 34 NaN 64 NaN 21];
Thanks
0 commentaires
Réponse acceptée
Daniele Mascali
le 12 Déc 2018
One possible solution:
FinalArray = nan(size(A));
FinalArray(find(not(isnan(A)))) = B
2 commentaires
Walter Roberson
le 12 Déc 2018
More compactly:
FinalArray = nan(size(A));
FinalArray(~isnan(A)) = B;
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices 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!