I should calculate sample mean ms and variance

조회 수: 2 (최근 30일)
Ihor
Ihor 2022년 12월 4일
편집: Torsten 2022년 12월 4일
I should calculate sample mean ms and variance vs and for each I calculated ms and vs calculate their relative errors in percent, than print it.
I know, that output should be something like that:
but I got only this:
I don't understang what is going wrong.
% Here is my code
m = 10 ;
v = 3 ;
for N = [1 2 5 10.^[ 1 : 5 ] ]
ms = mean( N ) ; % sample mean
vs = var( N ) ; % sample variance
rems = abs(ms-0.5)/0.5 * 100 ;
revs = abs(vs-1/12)/(1/12) * 100 ;
fprintf("Vector length N: %6d, mean: %10.6f, variance: %10.6f, mean error: %10.6f%%, variance error: %10.6f%% \n", N, ms, vs, rems, revs)
end
  댓글 수: 1
Torsten
Torsten 2022년 12월 4일
편집: Torsten 2022년 12월 4일
rems = abs(ms-0.5)/0.5 * 100 ;
revs = abs(vs-1/12)/(1/12) * 100 ;
0.5 as mean and 1/12 as variance are the theoretical values for mean and variance of a uniform distribution on the interval [0 1].
Of course, you will have to change them and insert here the theoretical values for mean and variance of your data arrays.

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

답변 (1개)

Walter Roberson
Walter Roberson 2022년 12월 4일
for N = [1 2 5 10.^[ 1 : 5 ] ]
at each point inside the loop, N will be a scalar value that is one of the values indicated
ms = mean( N ) ; % sample mean
mean of a scalar is the value itself.
vs = var( N ) ; % sample variance
variance of a scalar is 0.
  댓글 수: 1
Ihor
Ihor 2022년 12월 4일
Thank you! What should I do to solve this problem?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by