MATLAB: Aggregate every Nth row of a matrix

aggregatereshapesum

Hi everyone
I want to aggregate every Nth row of my X by Y matrix, such that the matrix transforms into a N by Y matrix
Example: aggregate every 3rd row of a 6 by 3 matrix A, transforming A into a 3 by 3 matrix B.
A=
1 6 8
8 6 4
2 5 12
6 8 7
14 8 6
7 21 5
B=
7 14 15
22 14 10
9 26 17
I have a 2464×44 matrix (F), of which I want to aggregate every 56th row, such that the matrix transforms into a 56×44 matrix. I know I can find the sum of every 56th element through the command: sum(F(1:56:end,:)1);
I just don't know how do this for each row and how to create the matrix that I need.
Thanks a lot in advance!!

Best Answer

Try this
A = [ ...
1 6 8
8 6 4
2 5 12
6 8 7
14 8 6
7 21 5];
n = 3;
B = zeros(n, size(A,2));
for i=1:n
B(i,:) = sum(A(i:n:end, :));
end
Result
>> B
B =
7 14 15
22 14 10
9 26 17
Alternative solution without for-loop
n = 3;
m = size(A, 1)/n;
idx = repmat(1:n, 1, m);
splitapply(@(x) sum(x), A, idx.')