How to calculate of average numbers every n rows?

36 ビュー (過去 30 日間)
Shaochen Hu
Shaochen Hu 2019 年 8 月 11 日
コメント済み: Lucas Maes 2021 年 10 月 22 日
For example I have a 30*10 matrix, I want to calculate a averager number for each column every 3 rows. The result should be a 10*10 matrix.

採用された回答

John D'Errico
John D'Errico 2019 年 8 月 11 日
編集済み: John D'Errico 2019 年 8 月 11 日
Pretty easy. What you need to learn is how MATLAB stores numbers in an array, how to reshape things so that you get what you want.
For example, suppose you reshaped the array to be 3x10x10? Essentially, then each column of the new result will be the three numbers that you want to average. The idea is to bring the numbers you want to associate into ONE column, with each set as one column.
Then you would take the mean of each solumn of that array. The result would be 1x10x10, and we will have averaged the elements together that you wanted averaged.
Getting close. Now all you need do is reshape that result to be 10x10. Or, you could learn to use the squeeze function, and why that function is valuable.
The point is to visualize how the data lives in your array. Then think about how to get where you want to go. So this should work:
B = squeeze(mean(reshape(A,[3,10,10]),1));
  4 件のコメント
EP
EP 2021 年 9 月 22 日
Can you take the average and keep the same number of columns? Just set both columns equal to the average?
Lucas Maes
Lucas Maes 2021 年 10 月 22 日
Nice explanation, thanks :)

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

その他の回答 (1 件)

WENLI QIAO
WENLI QIAO 2019 年 11 月 15 日
It is incredible! CRAZY LIKE!!!!!

カテゴリ

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