1,319 views (last 30 days)

Show older comments

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
on 27 Jun 2013

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

Jan
on 28 Jun 2013

Edited: Jan
on 17 May 2019

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

Stelios Fanourakis
on 19 May 2019

@Jan

Thanks for your thoroughtly explanation on your code.

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);

Although, it is consist of 6 simple code lines I wasn't familiar with the though behind it.

For instance, in this case, as your example data you used a vector of 399277 rows in a single column. You chose to find the mean per 1000 elements (n). I didn't quite understand why you are trying to find the next smaller multiple of n. I suppose you want integers to perfectly divide the x and not decimals. Am I correct?

You then reshape the matrix for faster computation, probably?

And what this transpose stands for? You calculate the mean over the first dimension of y, since the second one is the n. Right?

Always, welcome your explanations

Image Analyst
on 27 Jun 2013

Edited: Image Analyst
on 5 Jan 2019

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)

Jan
on 16 May 2019

Look at the code: These numbers are the size of the variable blockAveragedDownSignal.

Andrei Bobrov
on 28 Jun 2013

Edited: Andrei Bobrov
on 28 Jul 2017

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);

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

Start Hunting!