Placing none NaN values from a matrix to another

1 回表示 (過去 30 日間)
Santos García Rosado
Santos García Rosado 2021 年 3 月 26 日
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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by