How can I sum every nth row?

36 visualizaciones (últimos 30 días)
AJ
AJ el 8 de Jul. de 2018
Comentada: Yongqi Shi el 3 de Oct. de 2022
If there's a matrix, for example:
A = [1 2 3 4 5 6 ; 1 3 5 7 9 11 ; 2 4 6 8 10 12]'
A =
1 1 2
2 3 4
3 5 6
4 7 8
5 9 10
6 11 12
7 13 14
I'm trying to sum 3 rows at a time (like an attached jpg file).
So I want to get:
B =
6 9 12
15 27 30
7 13 14
Thanks a lot!
  2 comentarios
madhan ravi
madhan ravi el 8 de Jul. de 2018
Can you elaborate which row you want to sum in this example?
AJ
AJ el 8 de Jul. de 2018
for example, in column 1 in A,
1+2+3= 6, 4+5+6= 15, and 7 doesn't have pairs so it's just 7.
Then column 1 in B goes 6, 15, 7.
Well.. I'm not sure this is enough.. Sorry for the late reply!

Iniciar sesión para comentar.

Respuesta aceptada

Paolo
Paolo el 8 de Jul. de 2018
Editada: Paolo el 8 de Jul. de 2018
A = [1 2 3 4 5 6 7; 1 3 5 7 9 11 13; 2 4 6 8 10 12 14]';
[n,col] = size(A);
index = 1:n;
elem = [repmat(3,1,floor(n/3))];
endv = n-sum(elem);
if(~endv)
endv = [];
end
index = mat2cell(index,1,[elem,endv])';
B = cell2mat(cellfun(@(x) sum(A(x,:),1),index,'un',0));
B =
6 9 12
15 27 30
7 13 14
  6 comentarios
AJ
AJ el 8 de Jul. de 2018
Ohhhhh Thanks a lot!!!
Paolo
Paolo el 8 de Jul. de 2018
You can say thank you to me and Stephen by accepting and voting for either one of the questions :)

Iniciar sesión para comentar.

Más respuestas (2)

Peng Li
Peng Li el 13 de Abr. de 2020
Editada: Peng Li el 13 de Abr. de 2020
Just a comment that this could be done without involving a cell, which is the least type that I'd like to use among others.
A = [1 2 3 4 5 6 7; 1 3 5 7 9 11 13; 2 4 6 8 10 12 14]';
sumEveryRow = 3;
intBlock = floor(size(A, 1) / sumEveryRow)*sumEveryRow;
temp = reshape(A(1:intBlock, :)', size(A, 2), sumEveryRow, []);
sumA = [squeeze(sum(temp, 2))'; sum(A(intBlock+1:end, :), 1)];
This should be quick than the two above answers, both involving a cell type.
a test below
A = [1 2 3 4 5 6 7; 1 3 5 7 9 11 13; 2 4 6 8 10 12 14]';
A = repmat(A, 1000, 1);
tic
sumEveryRow = 3;
intBlock = floor(size(A, 1) / sumEveryRow)*sumEveryRow;
temp = reshape(A(1:intBlock, :)', size(A, 2), sumEveryRow, []);
sumA = [squeeze(sum(temp, 2))'; sum(A(intBlock+1:end, :), 1)];
toc
tic
[n,col] = size(A);
index = 1:n;
elem = [repmat(3,1,floor(n/3))];
endv = n-sum(elem);
if(~endv)
endv = [];
end
index = mat2cell(index,1,[elem,endv])';
B = cell2mat(cellfun(@(x) sum(A(x,:),1),index,'un',0));
toc
tic
N = 3;
S = size(A);
V = N*ones(1,ceil(S(1)/N));
V(end) = 1+mod(size(A,1)-1,3);
C = mat2cell(A,V,S(2));
Z = cellfun(@(m)sum(m,1),C,'uni',0);
toc
Elapsed time is 0.008036 seconds.
Elapsed time is 0.039083 seconds.
Elapsed time is 0.022779 seconds.
I repeated A 100 times to amphasize the effect of computational time.

Stephen23
Stephen23 el 8 de Jul. de 2018
A = [1 2 3 4 5 6 7; 1 3 5 7 9 11 13; 2 4 6 8 10 12 14]'
N = 3;
S = size(A);
V = N*ones(1,ceil(S(1)/N));
V(end) = 1+mod(size(A,1)-1,3);
C = mat2cell(A,V,S(2));
Z = cellfun(@(m)sum(m,1),C,'uni',0);
Giving
>> Z{:}
ans =
6 9 12
ans =
15 27 30
ans =
7 13 14
  1 comentario
AJ
AJ el 8 de Jul. de 2018
wow here's an another idea! Thank you!!

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by