Trouble with Mean function

1 view (last 30 days)
Alexandra Brian
Alexandra Brian on 9 May 2017
Commented: Star Strider on 9 May 2017
Hi, I wrote the following code, but it is returning NaN for the output of AvgRule1. It should be the mean of X, when A < 1. I have attached the spreadsheet and the code I used to this post. Please let me know if you can help. Also, there is no NAN data in the spreadsheet. Here's the code:
clear all
sheet = 1;
A = xlsread('example1.xlsx', sheet, 'C:C'); %Choose row
X = xlsread('example1.xlsx', sheet, 'B:B');%Choose row
Y = xlsread('example1.xlsx', sheet, 'A:A');%Choose row
P2SRule1 = X(A<1); %select X values where rule is true
AvgRule1 = mean(P2SRule1); %mean of matrix
avgDaysHeldRule1= mean(Y(A<1)); %Avg days heald where data meets conditions
A = { AvgRule1, avgDaysHeldRule1}; %create a variable with all data needed for spreadsheet
sheet = 2; %sheet for desired output
xlRange = 'B2'; %desired range for output
xlswrite('example1.xlsx', A, sheet, xlRange) %add data to spreedsheet

Accepted Answer

Star Strider
Star Strider on 9 May 2017
If you have a recent version of MATLAB, you can use mean with the 'omitnan' flag:
M = mean(A,'omitnan')
otherwise, this produces the same result:
N = mean(A(~isnan(A)))
  4 Comments
Alexandra Brian
Alexandra Brian on 9 May 2017
Thank you Star Strider!
Star Strider
Star Strider on 9 May 2017
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!