Extract and average from matrix

1 回表示 (過去 30 日間)
Joseff Saunders
Joseff Saunders 2018 年 12 月 4 日
編集済み: Image Analyst 2018 年 12 月 4 日
Hello all, Im trying to extract every 7 rows of a matrix and average them (mean). As an example:
4.20
4.10
4.10
4
3.90
3.80
NaN
3.80
3.90
3.90
3.70
3.60
3.60
3.40
3.40
3.30
3.30
3.30
3.30
3.10
3.10
What code would i need to take the first 7 rows and average them, and to continually do this for the matrix i.e. average of rows 1:7 8:14 15:21
Thanks in advance.

採用された回答

Bob Thompson
Bob Thompson 2018 年 12 月 4 日
The lowest difficulty way of doing this, that I can think of, is to use a for loop.
for i = 1:length(data)/7;
ave(i) = mean(data(7*(i-1)+1:i*7));
end
If you're looking for a non-loop method you might try using reshape first, and then average each column.
data2 = reshape(data,7,[]);
ave = mean(data2);
  2 件のコメント
Joseff Saunders
Joseff Saunders 2018 年 12 月 4 日
Brilliant, thank you
Image Analyst
Image Analyst 2018 年 12 月 4 日
編集済み: Image Analyst 2018 年 12 月 4 日
The second method (same as my answer below) is clever if you've never seen it before.
We see this question (and the answer) all the time - I guess I should add it to the FAQ.
EDIT: OK, I've added it to the FAQ: How do I compute the mean of a vector in blocks?

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 12 月 4 日
Try reshape()
vec = (1 : 70)' % Sample data
m2d = reshape(vec, 7, [])
means7 = mean(m2d, 1)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by