How to sum a specified range of rows in a matrix?
1 次查看(过去 30 天)
显示 更早的评论
Hi I'm tryign to sum up sections of a 5000x6482 matrix. I'd like to sum the 1st row with the 17th row, 33rd row and so so on in step sizes of 16 up to 5000. And 2nd row with the 18th row, 34th row etc.
I have tried the code below;
for j = 1:5000
for n = 1:16:6482
test(j,n) = sum(raw_signal(j,n));
end
end
This gives me a 4993x6482 matrix with values in the first row, 17th row, 33rd row... in stepsizes of 16. But the values have just been copied from the orignal matrix without any summation. I think there is more simple solution to this but I can't seem to get it to work. Any help would be much appreciated.
Thanks in advance.
回答(1 个)
Andrei Bobrov
2017-10-4
Let A - your array [5000x6482]
d = 16;
ii = rem((0:size(A,1)-1)',d)+1;
[x,y] = ndgrid(ii,1:size(A,2));
out = accumarray([x(:),y(:)],A(:));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!