How to take an average of row elements in a two-column matrix when both elements do not contain a NaN, but use only the non-NaN value when there is a NaN?
1 次查看(过去 30 天)
显示 更早的评论
I have a matrix with two columns of data. The data are measurements collected at the same times from different sensors, TT1 and TT2. I want to take an average of each row. My problem is dealing with NaN values. The column of data from sensor TDT2 contains many NaNs.
I know that I can use "nanmean" to ignore the rows where Column TDT2 contains a NaN. However, what I want to do is use only data from sensor TT1 (column 1) when there is a NaN from sensor 2 (column 2). In other words, I want to ignore the existence of column 2 when it contains a NaN.
Unfortunately, I can't figure out how to do this. If anyone has any suggestions, I would be very grateful. Thank you.
Here is an example (below) of a starting matrix and what I would want to end up with as a result:
2 2 --> 2
4 NaN 4
9 1 5
0 个评论
采纳的回答
Bruno Luong
2020-8-26
>> A=[2 2; 4 NaN; 9 1]
A =
2 2
4 NaN
9 1
>> mean(A,2,'omitnan')
ans =
2
4
5
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computational Geometry 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!