Placing none NaN values from a matrix to another

1 次查看(过去 30 天)
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

采纳的回答

the cyclist
the cyclist 2021-3-26
编辑:the cyclist 2021-3-26
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by