ignore NaN value in the matris
조회 수: 9 (최근 30일)
이전 댓글 표시
I want to ignore NaN values in my matrix. But I don't want it to sum or average the matrix. I just want it to operate with existing values, ignoring values that NaN in the matrix. Can you help me?
댓글 수: 7
Dyuman Joshi
2023년 11월 3일
편집: Dyuman Joshi
2023년 11월 7일
@Ali Topal, What error(s) do you get with NaN values?
Rik
2023년 12월 18일
If you explain what exactly you want to do, we might be able to suggest a solution. The constraints you describe make a solution impossible, so any solution will require breaking one of your requirements (but that might not actually be a problem).
답변 (1개)
Jeremy Hughes
2024년 1월 4일
There are a few ways to do this. Let's make an example vector with NaNs
x = rand(1,10);
x(1:4:end) = NaN
Now, for SUM, you probably just want to omit the NaNs using logical indexing.
x(~isnan(x)) % Returns only the non-NaN values
sum(x(~isnan(x)))
sum(x,"omitnan")
Not every function has an "omitnan" flag, so the first example would work when that's not the case.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!