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;

回答(1 个)

Alan Stevens
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by