Moving Average with timestep
Show older comments
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 Comments
Enrico Gambini
on 8 Feb 2022
Edited: Enrico Gambini
on 8 Feb 2022
v=movmean(M,2); %mean over a sliding window of length 2 across neighboring elements of M
Is this the answer?
MakM
on 8 Feb 2022
Jan
on 8 Feb 2022
This is exactly what the shown code does. If you want to omit each second output:
v = v(1:2:end)
MakM
on 9 Feb 2022
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
[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
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
[mean(M(1:2)) mean(M(3:4)) mean(M(5:6)) mean(M(7:8))] % same thing
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
on 9 Feb 2022
DGM
on 9 Feb 2022
Provide an example of what the expected output should be for the given vectors.
MakM
on 9 Feb 2022
Jan
on 9 Feb 2022
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.
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).
MakM
on 9 Feb 2022
Jan
on 9 Feb 2022
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
on 9 Feb 2022
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!