How to sum a specified range of rows in a matrix?

조회 수: 1 (최근 30일)
William Royle
William Royle 2017년 10월 4일
답변: Andrei Bobrov 2017년 10월 4일
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
KSSV
KSSV 2017년 10월 4일
You are calling sum on a single number.....how you expect sum?

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

답변 (1개)

Andrei Bobrov
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(:));

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by