Trouble with Mean function

조회 수: 1 (최근 30일)
Alexandra Brian
Alexandra Brian 2017년 5월 9일
댓글: Star Strider 2017년 5월 9일
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
Alexandra Brian
Alexandra Brian 2017년 5월 9일
Thank you Star Strider!
Star Strider
Star Strider 2017년 5월 9일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by