I have array and want to calculate mean every five element in this array and replace every five element by their mean value

2 ビュー (過去 30 日間)
Hello
I have different matrixes and size of those matrixes differ as well
Generally I want to do same thing on these arrays
For instance in case of array that consists of 3000 element, I want to calculate mean of every 5 element in this array and write this one single mean value instead of 5 elements
Shortly I want to calculate every next 5 element mean and replace those elements with this mean value
Can anyone provide me with corresponding code?

回答 (1 件)

Bruno Luong
Bruno Luong 2022 年 8 月 25 日
編集済み: Bruno Luong 2022 年 8 月 25 日
A = 1:100
A = 1×100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
n = 5;
Astair = repelem(mean(reshape(A,n,[]),1),1,n)
Astair = 1×100
3 3 3 3 3 8 8 8 8 8 13 13 13 13 13 18 18 18 18 18 23 23 23 23 23 28 28 28 28 28
plot(A)
hold on
plot(Astair)
  3 件のコメント
Lia Kirtadze
Lia Kirtadze 2022 年 8 月 25 日
移動済み: Rik 2022 年 8 月 25 日
and also my matrix is vertical
Bruno Luong
Bruno Luong 2022 年 8 月 25 日
編集済み: Bruno Luong 2022 年 8 月 25 日
Truncation if the length of the original vector if not divisible by n:
Astair = repelem(mean(reshape(A(1:n*floor(end/n)),n,[]),1),1,n);
Tip to reshape result in column
Astair = reshape(Astair,[],1);

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by