I have a column vector that is 9985x1. I want to create a new vector that lists the average of every 6 rows in the column. How can I do this? Is there a way to do it with a loop?
Thank you!

2 commentaires

Jan
Jan le 13 Nov 2017
"of every 6 rows" or "of every 6th row"?
farkab95
farkab95 le 13 Nov 2017
The average of rows 1 to 6 and then rows 7 to 12 and then 13 to 18 etc. Thanks!

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 13 Nov 2017
Modifié(e) : Jan le 4 Avr 2019

5 votes

This is the mean over 6 subsequent rows:
x = rand(9985, 1);
S = numel(x);
xx = reshape(x(1:S - mod(S, 6)), 6, []);
y = sum(xx, 1).' / 6;
The trailing rows are ignored.
[EDITED] General method for matrices:
x = rand(9985, 14);
p = 6;
n = size(x, 1); % Length of first dimension
nc = n - mod(n, p); % Multiple of p
np = nc / p; % Length of result
xx = reshape(x(1:nC, :), p, np, []); % [p x np x size(x,2)]
y = sum(xx, 1) / p; % Mean over 1st dim
y = reshape(y, np, []); % Remove leading dim of length 1

6 commentaires

Gervasio Batista
Gervasio Batista le 30 Nov 2017
Hi, how would you do this for several columns in the same matrix using a for loop? Could you provide an example?
Thanks!
Do you mean that x is a matrix and you want to reduce the size in the 1st dimension only? Then:
x = rand(9985, 16);
S = size(x, 1);
xx = reshape(x(1:S - mod(S, 6), :), 6, S/6, []);
y = squeeze(sum(xx, 1) / 6);
Parth Swaroop
Parth Swaroop le 9 Oct 2018
I have an (n,m) matrix. I want to reduce this matrix size by averaging upto every pth element. How can i modify your code for this?
@Parth Swaroop: I've posted the code to sum up every 6 elements. You want every p elements. Then you have to replace each 6 by p:
n = 9985;
m = 16;
x = rand(n, m);
xx = reshape(x(1:n - mod(n, p), :), p, n/p, []);
y = squeeze(sum(xx, 1) / p);
Jan
Jan le 4 Avr 2019
Modifié(e) : Jan le 4 Avr 2019
@vicsim: See [EDITED]
Ade Narayana
Ade Narayana le 11 Avr 2019
Super!! Thank you Jan!!

Connectez-vous pour commenter.

Plus de réponses (3)

Andrei Bobrov
Andrei Bobrov le 13 Nov 2017
Modifié(e) : Andrei Bobrov le 13 Nov 2017

2 votes

Let A - your array (9985 x 1).
out = splitapply(@mean,A,ceil((1:numel(A))'/6));
or
out = accumarray(ceil((1:numel(A))'/6),A(:),[],@mean);
Birdman
Birdman le 13 Nov 2017
mean(A(1:6:9985),:)

1 commentaire

Jan
Jan le 13 Nov 2017
This creates a vector of every 6th element of A at first. Then mean(AA, :) is most likely a typo and mean(A(1:6:end, :)) is meant.

Connectez-vous pour commenter.

Joe S
Joe S le 26 Déc 2018
These answers work when dim=1 but appears to fail for ndims =2 (multiple columns). To average nRows together (variable "gps") for ndims=2, see below. You have the option of including the average of the "modulus rows" with: includeMods=1.
%create simple matrix, easier to check vs. random numbers
nRows = 11;
nCols = 3;
includeMods=1;
x = reshape(1:(nRows*nCols),[nRows,nCols]);
%provide row number to average over
grps = 3;
%determine remainder rows from Modulo operation
exRows = mod(nRows,grps);
% work with the top portion that group "cleanly"
topM = x(1:end-exRows,:);
%gather dims for reshaping
nRowsTop = size(topM,1);
numTop = numel(topM);
%Reshape so nrows = grps (row number to average over)
flatTop = reshape(topM,[grps numTop./grps]);
%take averages
meanTop = mean(flatTop,1);
% reshape back to original number of columns
final = reshape(meanTop,[nRowsTop./grps nCols]);
%optional, average the modulus rows, add back into final answer
if includeMods
if ~(exRows==0)
exMat= x(end-exRows+1:end,:);
meanEx = mean(exMat,1);
final = vertcat(final,meanEx);
end
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by