Trouble with Mean function

1 visualización (últimos 30 días)
Alexandra Brian
Alexandra Brian el 9 de Mayo de 2017
Comentada: Star Strider el 9 de Mayo de 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

Respuesta aceptada

Star Strider
Star Strider el 9 de Mayo de 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 comentarios
Alexandra Brian
Alexandra Brian el 9 de Mayo de 2017
Thank you Star Strider!
Star Strider
Star Strider el 9 de Mayo de 2017
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by