Problem with mean function

조회 수: 2 (최근 30일)
Hampus
Hampus 2017년 2월 27일
댓글: James Tursa 2017년 3월 1일
Okey, so I have the two nx2 vectors "day" and "comp_r".
I want to calculate the mean for a certain day, say day 8.
condition = day(:,1) == 8;
avg_day_8 = mean( comp_r( condition, 1) );
The problem is that I get that the mean equals NaN, and I am sure that's a wrong answer.
So what am I doing wrong?

답변 (1개)

James Tursa
James Tursa 2017년 2월 27일
편집: James Tursa 2017년 2월 27일
What does this show:
sum(condition)
If sum(condition) is 0, then you will get a NaN result in the mean calculation. E.g.,
>> x = reshape(1:6,3,2)
x =
1 4
2 5
3 6
>> condition = x(:,1) < 1
condition =
0
0
0
>> mean(x(condition,1))
ans =
NaN
  댓글 수: 2
Hampus
Hampus 2017년 3월 1일
Yeah, but the "funny thing" is that i get an answer > 0, so there seems to be something wrong. Maybe I should re-install Matlab?
James Tursa
James Tursa 2017년 3월 1일
What does this show:
any(isnan(comp_r( condition, 1)))

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by