How to combine data with repeating groups of values?

조회 수: 1(최근 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.

채택된 답변

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 2013년 9월 9일
Thank you very much, this worked like a charm! As always, the best answers are most times the simplest.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by