gathering together specific elements from a column vector
조회 수: 1 (최근 30일)
이전 댓글 표시
HI all, I have the matrix
A=
identif pairs time allpairs
[1] 'PDL12' 'JF2008' [ NaN]
[1] 'PDL12' 'JF2009' [-1.0000e-004]
[1] 'PDL12' 'MA2009' [ -0.0330]
[1] 'PDL12' 'MJ2009' [ -0.0268]
[1] 'PDL12' 'JA2009' [ -0.0264]
[1] 'PDL12' 'SO2009' [ -0.1913]
[1] 'PDL12' 'ND2009' [ -0.0415]
[1] 'PDL12' 'JF2010' [ -0.0168]
[1] 'PDL12' 'MA2010' [ -0.0502]
[1] 'PDL12' 'MJ2010' [ -0.0761]
[1] 'PDL12' 'JA2010' [ -0.0429]
[1] 'PDL12' 'SO2010' [ -0.0548]
[1] 'PDL12' 'ND2010' [ -0.0173]
[1] 'PDL12' 'JF2011' [ 0.0071]
[1] 'PDL12' 'MA2011' [ 0.0121]
[1] 'PDL12' 'MJ2011' [ -0.0727]
[1] 'PDL12' 'JA2011' [ -0.1628]
[1] 'PDL12' 'SO2011' [ 0.0056]
[2] 'PDL13' 'JF2008' [ NaN]
[2] 'PDL13' 'JF2009' [ 0.0218]
[2] 'PDL13' 'MA2009' [ -0.0037]
[2] 'PDL13' 'MJ2009' [ 0.0120]
[2] 'PDL13' 'JA2009' [ -0.0027]
[2] 'PDL13' 'SO2009' [-5.0000e-004]
[2] 'PDL13' 'ND2009' [ -0.2818]
[2] 'PDL13' 'JF2010' [ -0.0216]
[2] 'PDL13' 'MA2010' [ -0.0330]
[2] 'PDL13' 'MJ2010' [ -0.0468]
[2] 'PDL13' 'JA2010' [ -0.1309]
[2] 'PDL13' 'SO2010' [ -0.0318]
[2] 'PDL13' 'ND2010' [ -0.0014]
[2] 'PDL13' 'JF2011' [ -0.0779]
[2] 'PDL13' 'MA2011' [ 0.0224]
[2] 'PDL13' 'MJ2011' [ -0.0662]
[2] 'PDL13' 'JA2011' [ -0.2187]
[2] 'PDL13' 'SO2011' [ -0.0745]
and I want to group all the observations that correspond to JF2008 (JF=January and February) as follows
1 PDL12 JF2008
2 'PDL13' 'JF2008' [ NaN]
then all the observations that correspond to the next months
[1] 'PDL12' 'MA2008' [ -0.0330]
[2] 'PDL13' 'MA2008' [ 0.0218]
and so forth... . And then for the year 2009 similarly to 2008
thanks in advance
댓글 수: 2
채택된 답변
Andrei Bobrov
2012년 5월 26일
eg
k = cellstr(reshape(datestr(datenum(0,1:12,1,0,0,0),'m'),2,[])')
t = cellfun(@(x){x(1:2) x(3:end)},A(:,3),'un',0)
tt = cat(1,t{:})
[a,a] = ismember(tt(:,1),k)
[id,id] = sortrows([a,str2double(tt(:,2))],[2,1])
out = A(id,:)
OR
in this is case
out = A(reshape(reshape(1:size(A,1),[],2)',[],1),:)
댓글 수: 5
Oleg Komarov
2012년 5월 27일
Say the matrix you posted is called A:
[trash idx] = sortrows([cat(1,A{:,1}) datenum(A(:,3),'mmm yyyy')],[2 1]);
A(idx,:)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!