Group Identical Values in Matrix While Maintaining Original Indicies
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Iain
2013년 5월 23일
I did that earlier with a not-fully-explicit answer and got told off for making it an answer...
the cyclist
2013년 5월 23일
편집: the cyclist
2013년 5월 23일
Hm. OK, I can see that point of view, too. Sorry you got "told off", though!
채택된 답변
the cyclist
2013년 5월 23일
편집: the cyclist
2013년 5월 23일
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.
추가 답변 (2개)
Sean de Wolski
2013년 5월 23일
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.
David Sanchez
2013년 5월 23일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!