Placing none NaN values from a matrix to another
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Santos García Rosado
le 26 Mar 2021
Commenté : Santos García Rosado
le 29 Mar 2021
Hello everybody,
I'm trying to get non NaN values from matrix A:
A = [NaN NaN NaN 3 0 NaN NaN 1; NaN 2 NaN 6 1 NaN NaN NaN; 5 NaN NaN 2 NaN NaN NaN 1]
Into Matrix B:
B = [ 1 2 3 4 5 6 7 8; 8 7 6 5 4 3 2 1; 2 4 6 8 10 12 14 16]
So that my Output looks like this:
Out = [ 1 2 3 3 0 6 7 1; 8 2 6 6 1 3 2 1; 5 4 6 2 10 12 14 1]
I tried with:
A(isnan(A)) = B;
But it ain't working. I could easily do it with a loop, but I'd like to avoid it.
Could somoene please give me a hand? Thank you,
Santos
0 commentaires
Réponse acceptée
the cyclist
le 26 Mar 2021
Modifié(e) : the cyclist
le 26 Mar 2021
A = [NaN NaN NaN 3 0 NaN NaN 1; NaN 2 NaN 6 1 NaN NaN NaN; 5 NaN NaN 2 NaN NaN NaN 1];
B = [ 1 2 3 4 5 6 7 8; 8 7 6 5 4 3 2 1; 2 4 6 8 10 12 14 16];
loc = ~isnan(A);
B(loc) = A(loc)
or if you do not want to change the value of B itself:
A = [NaN NaN NaN 3 0 NaN NaN 1; NaN 2 NaN 6 1 NaN NaN NaN; 5 NaN NaN 2 NaN NaN NaN 1];
B = [ 1 2 3 4 5 6 7 8; 8 7 6 5 4 3 2 1; 2 4 6 8 10 12 14 16];
Out = B;
Out(loc) = A(loc)
Plus de réponses (0)
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!