How to sum part of a matrix using loop command
조회 수: 2 (최근 30일)
이전 댓글 표시
I have matrix A dimension 35x1435 and I want to get matrix B dimension 35x35 that would have in cell 1,1 a sum of 1 row elements of coulumns 1+36+71+106+..+1401. (so distance is 35 ) and then this matrix B 1,2 cell entry would be a sum of first row elements of columns 2+37+72+107+..+1402 and the cell 1,3 of matrix B would be a sum of columns 3+38+ 73+..+1403 and the same logic for the whole B matrix (so entry 2,1 would be again a sum of 2 row elements 1+36+71+..1401 and the entry of 3,1 would sum the elements in row 3 of coulumns 1+36+71+..+1401)
I tried this loop but it is NOT giving me the correct 35x35 matrix:
for k1 = 1:35
for k2 = 1:35
B(k1,k2) = sum(A(k1,k2:3:end),2);
end;
end;
I followed the 'logic' of this example because here it worked correctly:
M = [4 3 6 8 1 10 5 2 9
1 10 3 9 4 2 5 7 6
9 5 1 7 8 10 3 4 2];
for k1 = 1:3
for k2 = 1:3
S(k1,k2) = sum(M(k1,k2:3:end),2);
end;
end;
댓글 수: 0
답변 (1개)
Alan Stevens
2021년 1월 20일
Try replacing
B(k1,k2) = sum(A(k1,k2:3:end),2);
by
B(k1,k2) = sum(A(k1,k2:35:end),2);
참고 항목
카테고리
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!