Placing none NaN values from a matrix to another

1 visualización (últimos 30 días)
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

Respuesta aceptada

the cyclist
the cyclist el 26 de Mzo. de 2021
Editada: the cyclist el 26 de Mzo. de 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)
B = 3×8
1 2 3 3 0 6 7 1 8 2 6 6 1 3 2 1 5 4 6 2 10 12 14 1
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)
Out = 3×8
1 2 3 3 0 6 7 1 8 2 6 6 1 3 2 1 5 4 6 2 10 12 14 1

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by