How do I take the average of every n values in a vector?

463 ビュー (過去 30 日間)
Brooks
Brooks 2013 年 6 月 27 日
I have some data on Pulse Rate and the sample was taken at 1000 Hz (One sample every millisecond), way too big for what I want to see. My vector is 399277x1 and I want to be able to average every 1000 values and get that number in a new vector of somewhere around 400x1. Is there any way to do this?
Thanks

採用された回答

Matthew Eicholtz
Matthew Eicholtz 2013 年 6 月 27 日
Try this...
n = 1000; % average every n values
a = reshape(cumsum(ones(n,10),2),[],1); % arbitrary data
b = arrayfun(@(i) mean(a(i:i+n-1)),1:n:length(a)-n+1)'; % the averaged vector
  9 件のコメント
Adoniram
Adoniram 2019 年 1 月 5 日
COOL! thank you
Richard Joisce
Richard Joisce 2022 年 2 月 28 日
@Image Analyst oh bore off ffs

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

その他の回答 (4 件)

Jan
Jan 2013 年 6 月 28 日
編集済み: Jan 2019 年 5 月 17 日
x = rand(399277, 1); % Some example data
n = 1000; % Number of elements to create the mean over
s1 = size(x, 1); % Find the next smaller multiple of n
m = s1 - mod(s1, n);
y = reshape(x(1:m), n, []); % Reshape x to a [n, m/n] matrix
Avg = transpose(sum(y, 1) / n); % Calculate the mean over the 1st dim
  10 件のコメント
Jan
Jan 2022 年 6 月 23 日
@Laukik Avinash Kharche: I assume, this helps:
Avg = repelem(Avg, 10, 1);
Laukik Avinash Kharche
Laukik Avinash Kharche 2022 年 7 月 1 日
Thanks a lot, it worked.
:)

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


Image Analyst
Image Analyst 2013 年 6 月 27 日
編集済み: Image Analyst 2019 年 1 月 5 日
Here is how I'd do it (an alternate way), using blockproc to average in 100 element long blocks in "jumps":
% Create sample data
PulseRateF = rand(399277, 1);
% Define the block parameter. Average in a 100 row by 1 column wide window.
blockSize = [1000, 1];
% Block process the image to replace every element in the
% 100 element wide block by the mean of the pixels in the block.
% First, define the averaging function for use by blockproc().
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:));
% Now do the actual averaging (block average down to smaller size array).
blockAveragedDownSignal = blockproc(PulseRateF, blockSize, meanFilterFunction);
% Let's check the output size.
[rows, columns] = size(blockAveragedDownSignal)
  5 件のコメント
Stelios Fanourakis
Stelios Fanourakis 2019 年 5 月 16 日
編集済み: Stelios Fanourakis 2019 年 5 月 16 日
I am not sure whether I understood it well. I use this
% Create sample data
PulseRateF = Pd;
% Define the block parameter. Average in a 100 row by 1 column wide window.
blockSize = [300, 100];
% Block process the image to replace every element in the
% 100 element wide block by the mean of the pixels in the block.
% First, define the averaging function for use by blockproc().
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:));
% Now do the actual averaging (block average down to smaller size array).
blockAveragedDownSignal = blockproc(PulseRateF, blockSize, meanFilterFunction);
% Let's check the output size.
[rows, columns] = size(blockAveragedDownSignal)
And I get
rows = 5 columns = 1
What those numbers mean?
Jan
Jan 2019 年 5 月 16 日
Look at the code: These numbers are the size of the variable blockAveragedDownSignal.

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


Andrei Bobrov
Andrei Bobrov 2013 年 6 月 28 日
編集済み: Andrei Bobrov 2017 年 7 月 28 日
x = randi(1000,399277,1);
n = 1000;
m = numel(x);
out = nanmean(reshape( [x(:);nan(mod(-m,n),1)],n,[]));
or
out = accumarray(ceil((1:numel(x))/1000)',x(:),[],@mean);
  3 件のコメント
Stelios Fanourakis
Stelios Fanourakis 2019 年 5 月 13 日
@Andrei Bobrov
What this actually does? Does it average in a step of n intervals along the x axis? This is what I am looking for. Hope this does what I want.
I want to divide the x axis into intervals and average between them.
Imran Yasin
Imran Yasin 2020 年 7 月 31 日
Very nice job!

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


Aubai
Aubai 2017 年 1 月 20 日
  1 件のコメント
Alexey
Alexey 2018 年 11 月 1 日
Great solution! Thanks!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by