How to combine data with repeating groups of values?

2 ビュー (過去 30 日間)
John
John 2013 年 9 月 9 日
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.

採用された回答

Arthur
Arthur 2013 年 9 月 9 日
So you want the first weekNumber of every week? You can use diff.
whatINeed = weekNumber([true; diff(weekNumber) ~=0])
  1 件のコメント
John
John 2013 年 9 月 9 日
Thank you very much, this worked like a charm! As always, the best answers are most times the simplest.
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by