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개)
Guillaume
2018년 11월 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'})
dpb
2018년 11월 2일
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
Kjell Lemmen
2018년 11월 2일
dpb
2018년 11월 2일
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...
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)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!