How can I calculate the mean of a vector ?

3 views (last 30 days)
How can I calculate the mean of a vector ?
V_freq1 = 2*abs(VOLT1(6:NFFT/64))./( 2*abs( DISP1 (6:NFFT/64) ).*(2*pi*10)^2);
VOLT1, disp1 are vectors.
Please help me Best,

Accepted Answer

KSSV on 12 Jul 2018
If your data has NaN's..have a look on nanmean.
% mean with inbuilt command
a = rand(100,1) ;
themean = 0 ; % the mean
for i = 1:length(a)
themean = themean+a(i) ;
themean = themean/length(a) ;
Guillaume on 12 Jul 2018
Steven, it's a long standing request of mine that this be documented in the function doc (just under the Introduced in ...). It would greatly help in writing backward compatible code.

Sign in to comment.

More Answers (1)

Jan on 12 Jul 2018
Edited: Jan on 12 Jul 2018
m = sum(V_freq1) / numel(V_freq1)
Good ideas to solve such problems:
docsearch mean
help mean
Internet search engine: "Matlab mean"
For old Matlab versions and ignoring NaNs:
index = ~isnan(v);
m = sum(v(index)) / sum(index)


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by