How to calculate mean and variance?

49 ビュー (過去 30 日間)
SHRIVATHSA S.
SHRIVATHSA S. 2017 年 1 月 19 日
回答済み: Nikos Lambadakis 2022 年 4 月 1 日
Greetings. I have generated a vector of 10,000 complex samples. I want to calculate the Mean and Variance of the samples. How can I do it without using the built-in functions _ mean()_ and * var()* ? and how do I do that in a loop? Please help.
Thank you
  1 件のコメント
Ana Soph
Ana Soph 2020 年 5 月 6 日
How can convert to 1 minute to 10 minutes, a wind directio data ? please, i have a matrix fo 1440x31

サインインしてコメントする。

採用された回答

Jorge Mario Guerra González
Jorge Mario Guerra González 2017 年 1 月 19 日
編集済み: Jorge Mario Guerra González 2017 年 1 月 19 日
Since you want to do it without using the functions, just do:
A=rand(1000,1); %your array
sum1=0;
for i=1:length(A)
sum1=sum1+A(i);
end
M=sum1/length(A); %the mean
sum2=0;
for i=1:length(A)
sum2=sum2+ (A(i)-M)^2;
end
V=sum2/length(A); %Varaince
  4 件のコメント
SHRIVATHSA S.
SHRIVATHSA S. 2017 年 1 月 19 日
Thank you @Jorge Guerra for the answer. It was really helpful
Jorge Mario Guerra González
Jorge Mario Guerra González 2017 年 1 月 19 日
編集済み: Jorge Mario Guerra González 2017 年 1 月 19 日
@Stephen Cobeldick absolutely agree, that was just a fast script I wrote to show the proccedure. I edited the answer

サインインしてコメントする。

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2017 年 1 月 19 日
Mean is the average -- the sum divided by the number of entries.
Variance is the sum of the squares of (the values minus the mean), then take the square root and divided by the number of samples
You can vectorize the calculation using sum().
To use a for loop to calculate sums, initialize a running total to 0, and then each iteration of the loop, add the current value to the running total.
  3 件のコメント
Jurgen
Jurgen 2017 年 12 月 19 日
This answer contradicts the variance formula on the MATLAB website (no square root). Oh and wikipedia disagrees. The MATLAB variance assumes a uniform probability distribution. A very important assumption.
John D'Errico
John D'Errico 2020 年 7 月 24 日
@ Jurgen - you make no sense in some of what you said, although you are correct about the square root being incorrect in this answer. Variance has no square root in it.
A variance is something you can compute from the data, or for a population, but there is no assumption about the underlying distribution, nor is there any need to make such an assumption.

サインインしてコメントする。


Olayemi Akinsanya
Olayemi Akinsanya 2017 年 9 月 16 日
編集済み: Olayemi Akinsanya 2017 年 9 月 16 日
P=[10:2:70]
i wrote a script for variance, it gives me a value of 320. when i check with var(P), it gives me a value of 330.6667. can someone advice what i did wrong.
  1 件のコメント
Walter Roberson
Walter Roberson 2017 年 9 月 16 日
You probably divided by N (number of items) instead of dividing by (N-1) (number of degrees of freedom). See https://stats.stackexchange.com/questions/100041/how-exactly-did-statisticians-agree-to-using-n-1-as-the-unbiased-estimator-for

サインインしてコメントする。


Nikos Lambadakis
Nikos Lambadakis 2022 年 4 月 1 日
A=rand(1000,1); %your array
sum((A(1:end)-sum(A(1:end))./length(A)).^2)/(length(A)-1) % the same result as
% var(A)
var(A) == sum((A(1:end)-sum(A(1:end))./length(A)).^2)/(length(A)-1)
ans =
logical
1

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by