Sum specific intervals of rows for a matrix

17 ビュー (過去 30 日間)
Timo
Timo 2018 年 2 月 20 日
コメント済み: Maryam S 2019 年 2 月 28 日
Hello everybody,
I know there are some similar posts regarding the topic but I was not able to solve my problem.
I have a matrix a=53320x228 and wanted to calculate the sum in intervals of 12 for each row (f.e. sum of elements 1:12,13:24... for each row), so that my output would be a 53320x19 matrix as each row has 19*12 elements. In the past I was always able to handle similar problems with:
reshape(sum(reshape(...)))
or
sum(reshape())
But unfortunately, this time I did not recognize how it will work. Other topics related to the problem also suggest to use reshape but it doesn't produce the desired results.
Hopefully someone could help me out, I would really appreciate each hint.

採用された回答

Aveek Podder
Aveek Podder 2018 年 2 月 23 日
Hi,
I believe you want to sum the 12 consecutive columns of a Matrix and convert it to one column. Doing this Matrix a of size 53320x228 results to a 53320x19 Matrix.
You need to shape the Matrix into a 53320x12x19 Matrix. Then you have to do column summation and reshape the matrix back to 5330x19 Matrix.
Please have a look at the following code:
B = reshape(a,53320,12,19);
C = sum(B,2);
out = reshape(C,53320,19);
Hope this helps.
  2 件のコメント
Timo
Timo 2018 年 2 月 26 日
編集済み: Timo 2018 年 2 月 26 日
Thank you so much, I was not aware of this simple possibility and "trick" :)!
Great answer, thanks again!
Greetings
Maryam S
Maryam S 2019 年 2 月 28 日
Hi Aveek, your answer was great hlep to me , thanks!

サインインしてコメントする。

その他の回答 (1 件)

Danielle Torp
Danielle Torp 2018 年 10 月 31 日
I have a follow up to this problem- If i wanted to calculate sums in intervals of differing lengths because each column has differing number of rows (i.e. column a = 100 rows, column b = 90 rows), but I want to reshape so each column only has 10 rows (which are sums of equal intervals per column).
  3 件のコメント
Danielle Torp
Danielle Torp 2018 年 11 月 1 日
Yes, the rest of the column would be blank.
Timo
Timo 2018 年 11 月 1 日
If your Matrix is m Then try sum(reshape(m,10,[]),2) since your Matrix must be filled with NaN or 0s to Form a regular grid. But Maybe i misunderstand u :)?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by