select y data with duration x data

Dear all, I have to select the y values in a duration of 5, with the same mechanism as a moving average. These are my arrays:
y = [95 93 93 94 95 96 93 92 94 95 95 95 93 94 94];
x = [ 1 1 2 1 3 1 3 2 1 2 1 1 2 1 3];
y is a numeric array and x a duration array in seconds
Now I have to select the y values who are in the 5 duration window. Once I have done that I have to the same starting from the second duration window etc.
Does anyone have the solution?
Thank you!

2 Comments

expected output?
Hello,
I want to get the y values in the 5 duartion window. So the duration is 5 by the 4th x value. starting from the second x value the duration window is 5 by the 4th x value. Do you understand? When I know this information i can select the points on the y vector with an index.

Sign in to comment.

Answers (2)

To calculate the mean of y over each interval of 5 duration:
accumarray(ceil(cumsum(x')/5), y', [], @mean)
To group the elements of y in each interval, as a cell array of vectors:
accumarray(ceil(cumsum(x')/5), y', [], @(vec) {vec'})
Yeah, need to explain more carefully; it's not at all clear what "in a duration of 5" is intended to mean.
If it's just, as the next phrase suggests, the moving average of five elements, and the questions revolves about the error using filter or other non-duration-aware functions on x, then the
N=5;
xfN=filter(ones(1,N)/N,1,seconds(x));
will solve that problem. Convert back to a duration if that's wanted by applying seconds the other direction--
xfN=seconds(filter(ones(1,N)/N,1,seconds(x)));
If that's the wrong guess; explain what is really intended.

3 Comments

Sorry for my bad expanation! First I have to say that I am building a filter where this is a part of. I have to remove outliers from the y vector in a duration window from 5 seconds. that does mean where the sum from the x vector is 5. Here is an example: 1 1 2 1 = 5 These are the first 4 elements from the x vector. So now i have to get the first 4 elements from y which are 95 93 93 94. Then the same thing starting by the second element from the x vector. So that is in this example 1 2 1 3 = 7 So also by the 4th element the sum from x >= 5. So now i need to get the 2 to 4th element from y which are 93 93 94. etc
Is this clear enough? I am sorry i can't give you my original data because of privacy reasons.
Guillaume's second solution will do that for each starting point at a time (altho in R2017b I still had to do the cast to double via seconds as accumarray still wasn't yet duration-aware; it may be later).
I don't see a way other than repeating for the alternate start locations at the moment, though...
Guillaume
Guillaume on 2 Nov 2018
Edited: Guillaume on 2 Nov 2018
Oh, so the windows are sliding instead of consecutive? That's a lot more tricky! I'll have to think about that. It may be that a loop is the only way.
Yes, I didn't see that x was a duration type. accumarray is still blissfully unaware of all the new types. It's trivial to cast to double.
Stuffing the data into a timetable may be a good idea if it already isn't.
edit: and the fact that it's to the first element where the sum is >= 5 rather than the last element at which the sum is <= 5 also makes it more tricky. Can the latter be used instead? (i.e 2nd window has length 3)

Sign in to comment.

Tags

Asked:

on 2 Nov 2018

Edited:

on 2 Nov 2018

Community Treasure Hunt

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

Start Hunting!