Vector averaging with mean command

4 ビュー (過去 30 日間)
Lev Mihailov
Lev Mihailov 2020 年 2 月 3 日
コメント済み: Lev Mihailov 2020 年 2 月 3 日
Hello! I don’t quite understand how to use this command, I need to average a 1x533 vector
Averaging1=mean(X,10);
Averaging2=mean(X,50);
Averaging3=mean(X,100);
I need to average over 10, 50 and 100, when I use such commands nothing happens
  2 件のコメント
KSSV
KSSV 2020 年 2 月 3 日
what do you mean by averaging over 10, 50 and 100?
Lev Mihailov
Lev Mihailov 2020 年 2 月 3 日
average vector by blocks of 10 and 50
X=[1:100]
Averaging1=mean(X,10);
Averaging1=[5.5 15.5 ....]

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

回答 (2 件)

Rik
Rik 2020 年 2 月 3 日
A faster method than a for-loop is using movmean and throwing out the results you don't need. This will also take care of the case where you have an incomplete trailing block.
clc
X=rand(28,1);
n=10;
tic
Averaging1=[mean(X(1:10)); mean(X(11:20)); mean(X(21:28))];
toc
tic
Averaging2=movmean(X,[0 n-1]);
Averaging2=Averaging2(1:n:end);
toc
isequal(Averaging1,Averaging2)
  3 件のコメント
Rik
Rik 2020 年 2 月 3 日
Do you mean this should be repeated for different values of n? Or do you have a vector with all group sizes?
%example for the latter:
n=[2 4 3];
Averaging1=[mean(X(1:2));mean(X(3:6));mean(X(7:9))]
Lev Mihailov
Lev Mihailov 2020 年 2 月 3 日
I got a vector of values ​​(necessary areas) that need to be averaged

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


Bhaskar R
Bhaskar R 2020 年 2 月 3 日
Second argument of mean is dimension, your input vector X is 1x533 vector 1D vector.
1) I assume you need mean of the values from 1 to 10, 1 to 50 and 1 to 100 values
Averaging1=mean(X(1:10));
Averaging2=mean(X(1:50));
Averaging3=mean(X(1:100));
or 2) I assume you need mean of the values from 1 to 10, 11 to 50 and 51 to 100 values then
Averaging1=mean(X(1:10));
Averaging2=mean(X(11:50));
Averaging3=mean(X(51:100));
  2 件のコメント
Lev Mihailov
Lev Mihailov 2020 年 2 月 3 日
編集済み: Lev Mihailov 2020 年 2 月 3 日
X=[1:1:553];
Averaging1=mean(X(1:10));
such a solution is good, but can you use a loop to make a vector along the entire length of the vector?
Averaging1=mean(X(1:10)); Averaging1=mean(X(11:21)); Averaging1=mean(X(22:32));
Rik
Rik 2020 年 2 月 3 日
You are ignoring the fact that your padding influences the mean value of the final block. You should probably use something like the code below.
if mod(numel(X),n)~=0
elem_count=numel(X);
X( (end+1):(n*ceil(numel(X)/n)) )=0;
X=reshape(X,n,[]);
divs=n*ones(1,size(X,2)-1);
divs(end+1)=mod(elem_count,n);
Averaging3=sum(X,1)./divs;
Averaging3=Averaging3';
else
X=reshape(X,n,[]);
Averaging3=mean(X,1);
Averaging3=Averaging3';
end

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by