sum every n columns, element by element
조회 수: 18 (최근 30일)
이전 댓글 표시
Isabel Aldana Benavides
2020년 4월 28일
댓글: Isabel Aldana Benavides
2020년 4월 28일
Hello,
I have for example,
m = randi(9, [10, 12]);
otuputRow=1;
for row = 1 : size(m, 2)
theSums(:, otuputRow) = sum(m(:,row:row+2:end),2);
otuputRow = otuputRow + 1;
end
but matlab show this error: Unable to perform assignment because the size of the left side is 3-by-1 and the size of the right side is 10-by-1.
I want sum columns every 3 columns (but element by element), so, the matrix resultant should be of 10x(12/3), this is a example but really the number of steps for sum can change (every 4, or every 5 columns...)
Thanks!!
댓글 수: 0
채택된 답변
James Tursa
2020년 4월 28일
m = your matrix
n = number of columns to sum
squeeze(sum(reshape(m,size(m,1),n,[]),2))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!