How to combine data with repeating groups of values?
2 次查看(过去 30 天)
显示 更早的评论
Okay, so I have an array called weekNumber. The array was made by taking hourly dates and assigning a week number. (In other words, weekNumber is the weekNumber of hourly date strings.)
An example of the data in weekNumber is:
weekNumber = [53;53;53;53;1;1;1;1;1;1;2;2;2;2;]
What I want to do is combine any repeated values so that weekNumber looks like this:
weekNumber = [53; 1; 2;]
I can do this using:
[sorted,idx] = sort(weekNumber);
[~,ij] = unique(sorted,'first');
Indx = (sort(idx(ij)));
However, my week number data extends back for three years and my code attempts to combine weeknumbers from different years.
For example, week number 53 from year 2011 and week 53 from year 2012 will be combined.
I don't want this to happen!
I want to combine week numbers from there respective years only. I want the range of week numbers containing week x in year y to be combined.
A clear example of what I mean is as follows:
whatIHave = [1;1;1;2;2;2;3;3;3;4;4;4;2;2;2;1;1;1;5;5;5;2;2;2;2;1;1;1;1;]
whatINeed = [1;2;3;4;2;1;5;2;1;]
Can someone please help?
I had difficulty explaining my problem so if you are confused, just ask and I will try to clarify.
0 个评论
采纳的回答
Arthur
2013-9-9
So you want the first weekNumber of every week? You can use diff.
whatINeed = weekNumber([true; diff(weekNumber) ~=0])
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!