How can I sum every nth row?

38 vues (au cours des 30 derniers jours)
AJ
AJ le 8 Juil 2018
Commenté : Yongqi Shi le 3 Oct 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 commentaires
madhan ravi
madhan ravi le 8 Juil 2018
Can you elaborate which row you want to sum in this example?
AJ
AJ le 8 Juil 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!

Connectez-vous pour commenter.

Réponse acceptée

Paolo
Paolo le 8 Juil 2018
Modifié(e) : Paolo le 8 Juil 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 commentaires
AJ
AJ le 8 Juil 2018
Ohhhhh Thanks a lot!!!
Paolo
Paolo le 8 Juil 2018
You can say thank you to me and Stephen by accepting and voting for either one of the questions :)

Connectez-vous pour commenter.

Plus de réponses (2)

Peng Li
Peng Li le 13 Avr 2020
Modifié(e) : Peng Li le 13 Avr 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.
  1 commentaire
Yongqi Shi
Yongqi Shi le 3 Oct 2022
excellent!

Connectez-vous pour commenter.


Stephen23
Stephen23 le 8 Juil 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 commentaire
AJ
AJ le 8 Juil 2018
wow here's an another idea! Thank you!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by