How to aggregate elements of a cell array in Matlab

4 ビュー (過去 30 日間)
Maria
Maria 2014 年 7 月 18 日
コメント済み: Maria 2014 年 7 月 18 日
I have a celL-type variable with 1 row and 17 columns that represent years. Each of the elements of the last is another cell-array with 15 columns and about 30 000 rows. These cell arrays, one for each year, are sorted by C2 and to the each firm match different codes (C3), for example:
C1 C2 C3 C4 C5
1997 AA 123 7863 0,4
1997 AA 45 3421 0,8
1997 GP 45 789 1,2
1997 GP 3 63 0,1
1997 GP 9124 597 0,9
1997 NU 123 7863 0,6
1997 NU 45 789 0
1997 NU 9124 81 0,8
1997 ZT 123 7863 0,4
1997 ZT 3 63 0,3
I am trying to aggregate the information based on C3, so at this example I would obtain:
C3 C1 C2 C4 C5
3 1997 GP 63 0,1
3 1997 ZT 63 0,3
45 1997 AA 3421 0,8
45 1997 GP 789 1,2
45 1997 NU 789 0
123 1997 AA 7863 0,4
123 1997 NU 7863 0,6
123 1997 ZT 7863 0,4
9124 1997 GP 597 0,9
9124 1997 NU 81 0,8
Can someone help me? Thank you.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 18 日
編集済み: Azzi Abdelmalek 2014 年 7 月 18 日
If you mean how to sort your data
v={'C1' 'C2' 'C3' 'C4' 'C5'
1997 'AA' 123 7863 0.4
1997 'AA' 45 3421 0.8
1997 'GP' 45 789 1.2
1997 'GP' 3 63 0.1
1997 'GP' 9124 597 0.9
1997 'NU' 123 7863 0.6
1997 'NU' 45 789 0
1997 'NU' 9124 81 0.8
1997 'ZT' 123 7863 0.4
1997 'ZT' 3 63 0.3}
w=v(:,[3 1 2 4 5])
out=[w(1,:); sortrows(w(2:end,:))]
If you were working with tables
w=cell2table(v(2:end,:),'VariableNames',v(1,:))
w=w(:,[3 1 2 4 5]),
out=sortrows(w,[3 1 2 4 5])
  1 件のコメント
Maria
Maria 2014 年 7 月 18 日
Thanks it worked,I just made a small change. I guess it is more simple like this, but I believe it's working. Thanks.
out= sortrows(w,1);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall C++ from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by