Averaging data every N rows

1 view (last 30 days)
Lilya
Lilya on 3 Jan 2016
Commented: Lilya on 4 Jan 2016
hi all, I have a matrix in dim of 8761*5 I want to average the data every 12 rows, starting from the 7th row to 19th and so on. the last 6 rows should be with the first 6 rows. the final dim should be 731*5
thanks for help.
  2 Comments
Image Analyst
Image Analyst on 3 Jan 2016
If you ignore the first 6 rows, then you have 8755 left. Taking groups of 12, you'd have 729.583333333333 sets of 12 because it's not a multiple of 12. So how do you get 731?
And we have no idea what "the last 6 rows should be with the first 6 rows." means. Does it mean group rows 1-6 (first 6) together with rows 8756-8761 (the last 6) when taking the mean?
Lilya
Lilya on 3 Jan 2016
That's the problem. Yes it is, the first 6 rows should be with the last 6 rows It's a half-daily data

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 4 Jan 2016
Try this, if you have the Image Processing Toolbox.
data = randi(9, 8772, 5); % Sample data.
% Start with the 7th row by doing an upward shift of 6 rows.
newData = circshift(data, -6, 1);
% Block process the matrix to give the mean of the pixels in a 12 row by 1 column block.
% The output will have 5 columns and 8772/12 = 731 rows.
meanFilterFunction = @(theBlockStructure) mean(theBlockStructure.data(:));
blockSize = [12, 1];
% Now compute the block means.
blockMeans = blockproc(newData, blockSize, meanFilterFunction)
[rows, columns] = size(blockMeans) % Just to double check the size.

More Answers (1)

Image Analyst
Image Analyst on 3 Jan 2016
Try
data = [data(6:end-6);data(1:6,:)];
or use circshift().

Tags

Community Treasure Hunt

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

Start Hunting!