I have an array M=[1,4,7,6,4.5,7.5,8.5,4.5] and for time t=[1,2,3,4,5,6,7,8]. I have to find the average of M w.r.t t, with a window size of 2 and step size of window should be 1 or 2. How can I do that?
I am using movmean function to calculate the average, how can I used the window and the step size in this function?

댓글 수: 13

Enrico Gambini
Enrico Gambini 2022년 2월 8일
편집: Enrico Gambini 2022년 2월 8일
v=movmean(M,2); %mean over a sliding window of length 2 across neighboring elements of M
Is this the answer?
MakM
MakM 2022년 2월 8일
Yes but I want the sliding window of length 2 to move with the displacement of 1.
This is exactly what the shown code does. If you want to omit each second output:
v = v(1:2:end)
MakM
MakM 2022년 2월 9일
I dont want to omit it. I want to calculate the average by using the windows of size 2 and this window should move with the displacement of 1.
It does move with a displacement of 1.
M = [1,4,7,6,4.5,7.5,8.5,4.5];
v = movmean(M,2) % this
v = 1×8
1.0000 2.5000 5.5000 6.5000 5.2500 6.0000 8.0000 6.5000
[M(1) mean(M(1:2)) mean(M(2:3)) mean(M(3:4)) mean(M(4:5)) ...
mean(M(5:6)) mean(M(6:7)) mean(M(7:8))] % is the same
ans = 1×8
1.0000 2.5000 5.5000 6.5000 5.2500 6.0000 8.0000 6.5000
Note that the first element represents the filter window being truncated. See the documentation for other options.
You also said you want it to move with a displacement of 2. Jan described that as well.
v = v(2:2:8) % assumed alignment
v = 1×4
2.5000 6.5000 6.0000 6.5000
[mean(M(1:2)) mean(M(3:4)) mean(M(5:6)) mean(M(7:8))] % same thing
ans = 1×4
2.5000 6.5000 6.0000 6.5000
If this isn't what you want, you need to explain why. If you have specific requirements for the window span or alignment, you need to communicate that information.
MakM
MakM 2022년 2월 9일
Thanks DGM for the information. But as I mentioned I need to average the M w.r.t time, lets say, I have time having values I mentioned in the question and M, then I have to enter the window size with displacement value, which should take average of M and T. I hope I have made the question a bit clear. :)
DGM
DGM 2022년 2월 9일
Provide an example of what the expected output should be for the given vectors.
MakM
MakM 2022년 2월 9일
Does this image make my question clear?
In the original question M was a vector. Is it a matrix now? The sketch does not clarify, what you want to achieve. Please post some code, which defines the procedure exactly, e.g.:
WindowsSize = 2;
Displacement = 1;
Data = rand(1, 10);
Output = [mean(Data(1:1+WindowSize-1)), ...
mean(Data((1:1+WindowSize-1)) + 1*Displacement), ...
mean(Data((1:1+WindowSize-1)) + 2*Displacement), ...
]
It is not clear what "with timestep" means.
DGM
DGM 2022년 2월 9일
편집: DGM 2022년 2월 9일
The figure only raises more questions. Therein, the sample window is shown as a 2x2 area between the t and M axes.
What exactly does this area represent? Are you trying to find the local average value of some function of M and t? Their union? sum? product? Are you trying to find some blockwise mean of the Toeplitz matrix formed from M and t?
Furthermore, what happens when the filter is moved? Where is it moved? Since we've established that 1 might equal 2 and 2 now means 2x2, will it still be a 2x2 square?
The clearest explanation is a simple concrete example.
P.S. If it is just the union of M and t, just do movmean((M+t)/2,2).
Timestep is actually the displacement with which the window should move. Now what I did is,
v=movmean(M,2); %taking window size 2
v=v(1:2:end); %taking the time step or displacement 2
t=t(1:2:end) %As omitted values for v, t values need to be omitted to plot them together
plot(t,v) %plot them together
Is thi correct?
Yes, this is what was suggested yesterday.
If the length of M is a multiple of 2, an equivalent code is:
v = (M(1:2:end) + M(2:2:end)) * 0.5;
t = t(1:2:end)
MakM
MakM 2022년 2월 9일
Thank you Jan, Kindly paste your answer, so I can accept :)

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

 채택된 답변

Jan
Jan 2022년 2월 9일
편집: Jan 2022년 2월 9일

0 개 추천

A simple average over 2 elements (length of M can be even or odd):
Len = numel(M);
v = (M(1:2:Len - rem(Len, 2)) + M(2:2:Len)) * 0.5;
t = t(1:2:end)

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2022년 2월 8일

편집:

Jan
2022년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by