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

채택된 답변

Bruno Luong
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개)

카테고리

Help CenterFile Exchange에서 Computational Geometry에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by