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 view (last 30 days)
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

Accepted Answer

Bruno Luong
Bruno Luong on 26 Aug 2020
>> A=[2 2; 4 NaN; 9 1]
A =
2 2
4 NaN
9 1
>> mean(A,2,'omitnan')
ans =
2
4
5

More Answers (0)

Categories

Find more on Computational Geometry in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!