If my two matrix are
a=3*3,b=3*3,c=3*3
a=[1 2 3,4 5 nan,7 nan 9]
b=[nan 3 3,4 5 6,nan 8 9]
c=a+b
Is that possible that c=[1 5 6,8 10 6,7 8 18]
How to calculate?
Thanks

 採用された回答

James Tursa
James Tursa 2017 年 5 月 16 日

1 投票

c = a + b;
c(isnan(c)) = a(isnan(c));
c(isnan(c)) = b(isnan(c));

5 件のコメント

Cam Salzberger
Cam Salzberger 2017 年 5 月 16 日
That should do it. Could also just pre-process "a" and "b" with:
a(isnan(a)) = 0;
b(isnan(b)) = 0;
c = a+b;
but then you'll end up with 0 in places that both "a" and "b" are NaN. I like James' answer better.
min wong
min wong 2017 年 5 月 17 日
thank you!
min wong
min wong 2017 年 5 月 17 日
But if want to average the matrix
Hoe to do it?
like c=(a+b)/2;
James Tursa
James Tursa 2017 年 5 月 17 日
Depending on what you want to have happen to the "NaN" spots, I am guessing you will want either
c = (a + b)/2;
c(isnan(c)) = a(isnan(c));
c(isnan(c)) = b(isnan(c));
or
c = (a + b)/2;
c(isnan(c)) = a(isnan(c))/2;
c(isnan(c)) = b(isnan(c))/2;
min wong
min wong 2017 年 5 月 18 日
Thanks !!

サインインしてコメントする。

その他の回答 (2 件)

Paulo Neto
Paulo Neto 2018 年 11 月 28 日

0 投票

How I can do this: c = (a + b)/b
Regards

3 件のコメント

James Tursa
James Tursa 2018 年 11 月 28 日
Did you mean element-wise division? c = (a+b)./b
Paulo Neto
Paulo Neto 2018 年 11 月 28 日
I'm considering a and b as arrays:
a=3*3,b=3*3,c=3*3
a=[1 2 3,4 5 nan,7 nan 9]
b=[nan 3 3,4 5 6,nan 8 9]
To calculate c = a + b, I'm using your routine:
c(isnan(c)) = a(isnan(c));
c(isnan(c)) = b(isnan(c));
and results in c=[1 5 6,8 10 6,7 8 18]
But I need to calculate: (a+b)./b, can I use the same method?
James Tursa
James Tursa 2018 年 11 月 28 日
What would you want the individual result to be if "a" is NaN, and if "b" is NaN?

サインインしてコメントする。

Paulo Neto
Paulo Neto 2018 年 11 月 28 日

0 投票

I'm considering a and b as arrays:
a=3*3,b=3*3,c=3*3
a=[1 2 3,4 5 nan,7 nan 9]
b=[nan 3 3,4 5 6,nan 8 9]
To calculate c = a + b, I'm using your routine:
c(isnan(c)) = a(isnan(c));
c(isnan(c)) = b(isnan(c));
and results in c=[1 5 6,8 10 6,7 8 18]
But I need to calculate: (a+b)./b, can I use the same method?

カテゴリ

タグ

質問済み:

2017 年 5 月 16 日

コメント済み:

2018 年 11 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by