How to apply moving harmonic average on an array?

조회 수: 3 (최근 30일)
Nisar Ahmed
Nisar Ahmed 2022년 7월 3일
편집: dpb 2022년 7월 4일
Hi,
I have attached the data Kd_i = (115*1), I want to keep first and last values as it is in the data (or also shown in the figure below) and in the row want compute the harmonic average of first and second values (red square), in the 3rd row I want to compute the harmonic average of second and thrid values (green square) asn so on...
I mean first and last values remain same (just to have final array/vector of equal length), and then from second row it moves the by computing the harmonic mean of first and second row, until second last row. Can someone help me to write it code?
Thanks

채택된 답변

Abhishek Tiwari
Abhishek Tiwari 2022년 7월 3일
Hi,
A 'For' loop and harmmean() may do the same thing, as shown.
n = numel(Kd_i);
harmonicMean = Kd_i;
for idx = 2:n-1
harmonicMean(idx) = harmmean(Kd_i(idx-1 : idx, 1));
end
  댓글 수: 3
Abhishek Tiwari
Abhishek Tiwari 2022년 7월 4일
Yeah, harmonicMean has first and last values from Kd_i, and the remaining are the harmonic means of two rows (prev. and current).
Nisar Ahmed
Nisar Ahmed 2022년 7월 4일
thanks

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Star Strider
Star Strider 2022년 7월 3일
If you want to calculate it as described in the harmmean More About section, perhaps something like this would work —
movhm = @(x) 2./movsum(1./x,2);
v = 1:5
v = 1×5
1 2 3 4 5
movhm(v)
ans = 1×5
2.0000 1.3333 2.4000 3.4286 4.4444
would work.
.

dpb
dpb 2022년 7월 3일
편집: dpb 2022년 7월 4일
Simplest is probably just brute-force --
HM=[Kd_1(1) arrayfun(@(i)harmmean(Kd_1(i:i+1)),1:numel(Kd_1)-1) Kd_1(end)].';
  댓글 수: 2
Nisar Ahmed
Nisar Ahmed 2022년 7월 4일
@dpb What is K here; harmmean(K(i:i+1)), is it the same Kd_i? and if I change as K = Kd_i, following error appears
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in Const_Q_Full_Mackbeth (line 90)
Kd_HM = [Kd_i(1); arrayfun(@(i)harmmean(Kd_i(i:i+1)),1:numel(Kd_i)-1);Kd_i(end)];
dpb
dpb 2022년 7월 4일
Yeah, I made a local array of K=Kd_1(1:15) so wasn't too much to look at in command window...then didn't fix -- and arrayfun returns row vector didn't transpose...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by