Trouble with Mean function

1 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2017-5-9
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 个评论
Star Strider
Star Strider 2017-5-9
As always, my pleasure!

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by