How to combine data with repeating groups of values?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Arthur
le 9 Sep 2013
So you want the first weekNumber of every week? You can use diff.
whatINeed = weekNumber([true; diff(weekNumber) ~=0])
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!