I have a cell-type variable X and I would like to have variable X separated in different variables for each different Firm (X(:,1). Meaning, everytime Firm name changes I get a new variable. Example:
X:
Firm
'ABC' 0,5 46 19970224
'ABC' 0,6 29 19970424
'EFD' 0,5 145 19970218
'EFD' 0,5 36 19970206
'GHI' 0,7 9 19971023
'LMN' 0,2 735 19970311
I would like to get:
X1:
'ABC' 0,5 46 19970224
'ABC' 0,6 29 19970424
X2:
'EFD' 0,5 145 19970218
'EFD' 0,5 36 19970206
X3:
'GHI' 0,7 9 19971023
X4:
'LMN' 0,2 735 19970311
Can anyone help? Considering my variable type is 'cell', with both strings and numbers.

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 25 日

2 投票

firm={'ABC' 0,5 46 19970224
'ABC' 0,6 29 19970424
'EFD' 0,5 145 19970218
'EFD' 0,5 36 19970206
'GHI' 0,7 9 19971023
'LMN' 0,2 735 19970311}
[a,b,c]=unique(firm(:,1))
out=accumarray(c,(1:numel(c))',[],@(x) {firm(x,:)})
out{1}
out{2}
out{3}
out{4}

5 件のコメント

Maria
Maria 2014 年 6 月 25 日
omg it worked perfectly! I don't think I would have ever arrived to such a simple but very very efficient code! thank you a lot!
Maria
Maria 2014 年 6 月 26 日
Do you know how can I know apply the sort/ or sortrows function for each new cell? I have a X cell and then I get X{1,1} cell and I am not being able to do the usual sort! Thanks!
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 26 日
out1=cellfun(@(x) sortrows(x),out,'un',0)
Maria
Maria 2014 年 6 月 26 日
It works! And I guess if I want to sort by a specif column I just add like this:
FAF3_97=cellfun(@(x) sortrows(x,[1, 13]),FAF2_97,'un',0);
At least it seems to be working! Thank you :)
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 26 日
This is correct

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Characters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by