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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by