ignore NaN value in the matris
32 次查看(过去 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 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!