Placing none NaN values from a matrix to another
1 view (last 30 days)
Show older comments
Santos García Rosado
on 26 Mar 2021
Commented: Santos García Rosado
on 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 Comments
Accepted Answer
the cyclist
on 26 Mar 2021
Edited: the cyclist
on 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)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!