Matrix index replance of non Nan value

1 回表示 (過去 30 日間)
Nikan Fakhari
Nikan Fakhari 2021 年 8 月 20 日
回答済み: Walter Roberson 2021 年 8 月 20 日
Hi there,
I have a matrix
x = [5 6 7;8 9 10; 11 12 NaN]
and another matrix with same as x,
y = [1 4 7;2 5 8; 3 0 9];
and I want to replace y with all the non nan vlaues of X
so for example I want y to be the following:
y = [5 6 9; 8 9 10;11 12 9];
Could anyone please help me with the right command for this?
I appreciate it a lot.
Best,
Nikan

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 8 月 20 日
x = [5 6 7;8 9 10; 11 12 NaN]
y = [1 4 7;2 5 8; 3 0 9];
mask = ~isnan(x);
y(mask) = x(mask);
Or you could do
newy = x;
mask = isnan(x);
newy(mask) = y(mask);

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by