Group Identical Values in Matrix While Maintaining Original Indicies
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Iain
am 23 Mai 2013
I did that earlier with a not-fully-explicit answer and got told off for making it an answer...
the cyclist
am 23 Mai 2013
Bearbeitet: the cyclist
am 23 Mai 2013
Hm. OK, I can see that point of view, too. Sorry you got "told off", though!
Akzeptierte Antwort
the cyclist
am 23 Mai 2013
Bearbeitet: the cyclist
am 23 Mai 2013
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.
Weitere Antworten (2)
Sean de Wolski
am 23 Mai 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 Kommentar
David Sanchez
am 23 Mai 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 Kommentare
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!