Trouble with Mean function

1 Ansicht (letzte 30 Tage)
Alexandra Brian
Alexandra Brian am 9 Mai 2017
Kommentiert: Star Strider am 9 Mai 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

Akzeptierte Antwort

Star Strider
Star Strider am 9 Mai 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 Kommentare
Alexandra Brian
Alexandra Brian am 9 Mai 2017
Thank you Star Strider!
Star Strider
Star Strider am 9 Mai 2017
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by