Ignoring NaN values when multiplying?

22 ビュー (過去 30 日間)
Anna
Anna 2011 年 4 月 22 日
回答済み: Steven Lord 2017 年 6 月 6 日
I need to multiply a range of elements with eachother, and now i discovered that if any of the elements is entered as NaN, the entire product will be NaN.
winnerportfolios=zeros(11,127);
for column=1:127
y=2;
x=firstWinnerRow(1,column);
for col=2:3:375
for nrStocks=10:10:100
winnerportfolios(y,column)=prod(HPRsorted(x:x+nrStocks,col));
y=y+1;
end
end
end
I'd like to find a way to ignore the NaN values. The only way i come to think of would be to change the NaN-values to 1...
Thank you so much in advance!
oh and ignore the awful code, I've been working with matlab for only about a month :)
  1 件のコメント
Federal Cearpa
Federal Cearpa 2017 年 6 月 6 日
Turn all the Nan's into 1's.

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

回答 (2 件)

Teja Muppirala
Teja Muppirala 2011 年 4 月 22 日
"The only way i come to think of would be to change the NaN-values to 1"
That sounds like a reasonable idea to me:
X = [2 3 4; nan 2 7; 2 5 nan]
Y = X;
Y(isnan(Y)) = 1;
prod(Y)
  2 件のコメント
Anna
Anna 2011 年 4 月 22 日
Thank you for your help, I'd better do it like that then :)
Jerry Gregoire
Jerry Gregoire 2015 年 4 月 14 日
Jonas had a good entry dealing with this issue and why you may want to consider the implications of doing this.

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


Steven Lord
Steven Lord 2017 年 6 月 6 日
If you're using release R2017a or later, use the 'omitnan' flag in your call to prod.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by