How can I sum every nth row?

조회 수: 39 (최근 30일)
AJ
AJ 2018년 7월 8일
댓글: Yongqi Shi 2022년 10월 3일
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
madhan ravi
madhan ravi 2018년 7월 8일
Can you elaborate which row you want to sum in this example?
AJ
AJ 2018년 7월 8일
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!

댓글을 달려면 로그인하십시오.

채택된 답변

Paolo
Paolo 2018년 7월 8일
편집: Paolo 2018년 7월 8일
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
AJ
AJ 2018년 7월 8일
Ohhhhh Thanks a lot!!!
Paolo
Paolo 2018년 7월 8일
You can say thank you to me and Stephen by accepting and voting for either one of the questions :)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Peng Li
Peng Li 2020년 4월 13일
편집: Peng Li 2020년 4월 13일
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 2018년 7월 8일
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
AJ
AJ 2018년 7월 8일
wow here's an another idea! Thank you!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by