# How to ask matlab to find mean only if there are less than 3 NaN values?

5 views (last 30 days)
MP on 5 Aug 2022
Commented: MP on 5 Aug 2022
How to ask matlab to find mean only if there are less than 2 NaN values in a given column, otherwise it should be NaN?
For Example,
qq = [1 2 3 NaN; 1 NaN NaN NaN];
qqm = mean(qq','omitnan');
qqm
% o/p is Value: [2,1], size: 1x2, Class: Double
I want to find output like:
qqm = [2, NaN]
Any help will be greatly appriciated.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Bruno Luong on 5 Aug 2022
qq = [1 2 3 NaN; 1 NaN NaN NaN];
qqm = mean(qq','omitnan');
qqm(sum(isnan(qq),2)>=2) = NaN;
qqm
qqm = 1×2
2 NaN
##### 1 CommentShowHide None
MP on 5 Aug 2022
Thank you so much!
That saved a lot time!!

Sign in to comment.

### Categories

Find more on Logical 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!