defining the mean of a values inside a matrix

1 回表示 (過去 30 日間)
Eddy Ramirez
Eddy Ramirez 2021 年 4 月 18 日
コメント済み: Khalid Mahmood 2021 年 4 月 19 日
Greetings,
I was trying to find a way to find the mean of the values inside the matrix. For example, [1,2,3,4,5,6] I want to find the average between 1 & 2/ 2&3/3&4/4&5 and so on
I tried to do a for-loop but It doesnt seemt to work when I compare the values to hand calculations
z18=(-length(theta)/2+(0:length(theta)))'*h; % Travel of total 18 layers %%%% 19 values due to neutral axis
z18t=((-length(theta)/2+(0:length(theta)))'*h)/2; %% I took this approach as well but it does not work
for t=1:length(z18)
tt=max(1,(t-1));
ttt=min(length(z18), t+1);
tttt(t)=mean(z18(tt:ttt));
end

採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 18 日
A = randi(9, [1 6])
A = 1×6
8 2 2 3 6 3
[A(1), A(1:end-1) + diff(A)/2, A(end)]
ans = 1×7
8.0000 5.0000 2.0000 2.5000 4.5000 4.5000 3.0000
  1 件のコメント
Eddy Ramirez
Eddy Ramirez 2021 年 4 月 18 日
this works as magic!
thank you

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

その他の回答 (1 件)

Khalid Mahmood
Khalid Mahmood 2021 年 4 月 18 日
編集済み: Khalid Mahmood 2021 年 4 月 18 日
Further methods
1: movmean(matrix,windowsize) it calculates running mean but considers only windowsize elements
examples:
mm2=movmean(z18,2)
mm3=movmean(z18,3)
Using for loop to calculate running mean as [z18(1), (z18(1)+z18(2)/2, ( (z18(1)+z18(2) )/2+ z18(3) )/3,...]
mm=z18(1);
for p=2:length(z18)
mm(p)=(mm(p-1)+z18(p))/p;
end
  8 件のコメント
Sufia Tanveer
Sufia Tanveer 2021 年 4 月 18 日
I learnt more from comments than actual question!
Khalid Mahmood
Khalid Mahmood 2021 年 4 月 19 日
And remember, In moving mean, last value of source vector is not copied in result. It is actually calculated. So moving mean methods, movmean, mm code (to rduce in a mean way), and vectorised cumsum./[1:src] methods are actual moving mean methods. Those are just different types of moving means

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by