Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Group Identical Values in Matrix While Maintaining Original Indicies

Asked by Nathan on 23 May 2013

Hi I have a matrix that is 43885x1. The matrix is composed of dates from 1/20/2009 to 2/20/2009. The matrix flows in numerically increasing order but with duplicates for each date. I want to group each day into its own variable with corresponding indicies. So for a matrix for 1/20/2009 would be all the indicies that originally were 1/20/2009 from the original large matrix, i.e.

X1=[ 1 2 3 ... 8567]

And for 2/20/2009 it would read something like

X32=[ 37568 37569 37570 .... 43885]

5 Comments

the cyclist on 23 May 2013

Nathan, how is your original matrix stored? Are the dates strings in a cell array, or datenums in a numerical array?

Iain on 23 May 2013

I did that earlier with a not-fully-explicit answer and got told off for making it an answer...

the cyclist on 23 May 2013

Hm. OK, I can see that point of view, too. Sorry you got "told off", though!

Nathan

Tags

Products

3 Answers

Answer by the cyclist on 23 May 2013
Edited by the cyclist on 23 May 2013
Accepted answer

Here is an example:

D = {'01/01/2001';'01/01/2001';'01/01/2001';'02/02/2002';'03/03/2003';'03/03/2003'};
[i,j,k] = unique(D);
numberDates = numel(i);
X = cell(numberDates,1);
for nd=1:numberDates
      X{nd} = find(k==nd);
end

Notice that I used a single cell array X{1}, X{2}, etc. to store your output instead of separate variables X1, X2, etc. You will find this is a much more workable approach.

1 Comment

Nathan on 23 May 2013

This works thanks again!

the cyclist
Answer by Sean de Wolski on 23 May 2013

Hi Nathan,

Here is the general workflow you will want to use:

  • Use datenum() to convert the dates to serial date numbers
  • Then, use unique() on this while capturing the first and thirs output:
[uv,~,idx] = unique(X);
  • uv will contain the unique values and idx will be the size of x with each of its values pointing to the corresponding unique value.

From here it depends on what you want to do with this information. You have the indices, idx, where each index represents the value. You can use these as the subs in accumarray to perform a function on some other data. A better detailed example with all inputs, and expected outputs would help us help you with this.

1 Comment

Nathan on 23 May 2013

So basically what I have here are 30 spreadsheets each in a similar format. 5 pairs of columns of a timestamp and 5 readings. The temperatures are from different meters and record at the same time. Then theres a sixth pair of coulums of a timestamp and a dew point based on these temperatures.

We first have to seperate each timestamp column by day and get the corressponding temps foreach index.

The goal in the end is to find when the dew point reaches a certain degrees C from as T increases during a heating cycle. And also find when the dew point reaches a different certain degrees C on the cooling cycle. And find the corresponding readings at this point from the other 5 meters.

Sean de Wolski
Answer by David Sanchez on 23 May 2013
A=[ 1;1;2;2;2;3;3;3;4;4;4]; % example matrix
[B ind]=unique(A); 
L = length(ind);
C = cell(L,1);
C{1} = A(1:ind(1));
for k = 2:L
     C{k} = A( (ind(k-1)+1):ind(k) );
end

You will end up with a cell array whose cells contain a matrix for each date

0 Comments

David Sanchez

Contact us