gathering together specific elements from a column vector

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 Comments

Do you want to sort the table according to the third and then second row?
adrei bobrov gave the solution. thank you as well

Sign in to comment.

 Accepted Answer

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 Comments

thank you. the first code fits my case
andrtei, I would also like to ask you something regarding the code
cellstr(reshape(datestr(datenum(0,1:12,1,0,0,0),'m'),2,[])')
you displayed above.
I want to do the same thing as before but for the following matrix
[1] 'PDL12' ' OCT 2008' [ 0.0073]
[1] 'PDL12' 'NOV 2008' [ 0.0100]
[1] 'PDL12' 'DEC 2008' [ 0.0149]
[1] 'PDL12' 'JAN 2009' [ 0.0081]
[1] 'PDL12' 'FEB 2009' [ 0.0055]
[1] 'PDL12' 'MAR 2009' [ 0.0099]
[1] 'PDL12' 'APR 2009' [ 0.0065]
[1] 'PDL12' 'MAY 2009' [ 0.0056]
[1] 'PDL12' 'JUN 2009' [ 0.0176]
[1] 'PDL12' 'JUL 2009' [ 0.0094]
[1] 'PDL12' 'AUG 2009' [ 0.0070]
[1] 'PDL12' 'SEP 2009' [ 0.0223]
[1] 'PDL12' 'OCT 2009' [ 0.0124]
[1] 'PDL12' 'NOV 2009' [ -0.0131]
[1] 'PDL12' 'DEC 2009' [ 0.0161]
[1] 'PDL12' 'JAN 2010' [ 0.0126]
[1] 'PDL12' 'FEB 2010' [ 0.0146]
[1] 'PDL12' 'MAR 2010' [ 0.0195]
[1] 'PDL12' 'APR 2010' [ 0.0378]
[1] 'PDL12' 'MAY 2010' [ 0.0414]
[1] 'PDL12' 'JUN 2010' [ 0.0356]
[1] 'PDL12' 'JUL 2010' [ 0.0276]
[1] 'PDL12' 'AUG 2010' [ 0.0860]
[1] 'PDL12' 'SEP 2010' [ 0.0258]
[1] 'PDL12' 'OCT 2010' [ 0.0354]
[1] 'PDL12' 'NOV 2010' [ 0.0375]
[1] 'PDL12' 'DEC 2010' [ 0.0366]
[1] 'PDL12' 'JAN 2011' [ 0.0203]
[1] 'PDL12' 'FEB 2011' [ -0.0078]
[1] 'PDL12' 'MAR 2011' [ -0.0158]
[1] 'PDL12' 'APR 2011' [ 0.0096]
[1] 'PDL12' 'MAY 2011' [ -0.0032]
[1] 'PDL12' 'JUN 2011' [ -0.0082]
[1] 'PDL12' 'JUL 2011' [ -0.0076]
[1] 'PDL12' 'AUG 2011' [ 0.0048]
[1] 'PDL12' 'SEP 2011' [ -0.0185]
[2] 'PDL13' ' OCT 2008' [ 0.0187]
[2] 'PDL13' 'NOV 2008' [ 0.0176]
[2] 'PDL13' 'DEC 2008' [ -0.0012]
[2] 'PDL13' 'JAN 2009' [ 0.0145]
[2] 'PDL13' 'FEB 2009' [ 0.0274]
[2] 'PDL13' 'MAR 2009' [ 0.0186]
[2] 'PDL13' 'APR 2009' [ -0.0106]
[2] 'PDL13' 'MAY 2009' [ -0.0034]
[2] 'PDL13' 'JUN 2009' [ 0.0230]
[2] 'PDL13' 'JUL 2009' [ 0.0231]
[2] 'PDL13' 'AUG 2009' [ 0.0030]
[2] 'PDL13' 'SEP 2009' [-7.0000e-004]
[2] 'PDL13' 'OCT 2009' [ 0.0015]
[2] 'PDL13' 'NOV 2009' [ -0.0241]
[2] 'PDL13' 'DEC 2009' [ 0.0227]
[2] 'PDL13' 'JAN 2010' [ 0.0133]
[2] 'PDL13' 'FEB 2010' [ 0.0110]
[2] 'PDL13' 'MAR 2010' [ 0.0142]
[2] 'PDL13' 'APR 2010' [ 0.0192]
[2] 'PDL13' 'MAY 2010' [ 0.0132]
[2] 'PDL13' 'JUN 2010' [ 0.0164]
[2] 'PDL13' 'JUL 2010' [ 0.0228]
[2] 'PDL13' 'AUG 2010' [ 0.0069]
[2] 'PDL13' 'SEP 2010' [ -0.0069]
[2] 'PDL13' 'OCT 2010' [ -0.0064]
[2] 'PDL13' 'NOV 2010' [ -0.0109]
[2] 'PDL13' 'DEC 2010' [ -0.0251]
[2] 'PDL13' 'JAN 2011' [ 0.0155]
[2] 'PDL13' 'FEB 2011' [ -0.0022]
[2] 'PDL13' 'MAR 2011' [ 0.0043]
[2] 'PDL13' 'APR 2011' [ 0.0132]
[2] 'PDL13' 'MAY 2011' [ 0.0113]
[2] 'PDL13' 'JUN 2011' [ -0.0026]
[2] 'PDL13' 'JUL 2011' [ 0.0011]
[2] 'PDL13' 'AUG 2011' [ 0.0131]
[2] 'PDL13' 'SEP 2011' [ 0.0142]
how can I do this using something similar to cellstr(reshape(datestr(datenum(0,1:12,1,0,0,0),'m'),2,[])')
thanks
actually i want to modify your code
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,:)
but I have problem doing it
thanks
Say the matrix you posted is called A:
[trash idx] = sortrows([cat(1,A{:,1}) datenum(A(:,3),'mmm yyyy')],[2 1]);
A(idx,:)
Thank you oleg. it works!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!