Ignoring NaN values when multiplying?
조회 수: 18 (최근 30일)
이전 댓글 표시
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 :)
답변 (2개)
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!