Track changes in a cell array with strings
5 ビュー (過去 30 日間)
古いコメントを表示
I have a cell array with 5 columns and about 5000 rows with string elements. E.g.:
1997 Charles House Materials Chemicals
The years go from 1997 to 2013 and you find repetitions while the years pass by. What I would like to get is a new cell array with the cases in which the combination of the second and third column change. E.g. original cell array:
1997 Charles House Materials Chemicals %initial
1997 Rita Office Financial Bank %initial
1998 Rita Office Financial Bank %no change
1999 Charles House Materials Chemicals %no change
2000 Charles Office Materials Chemicals %change in the 2nd column
2001 Charles Office Materials Chemicals %no change
2003 Rita Star Financial Bank %change in the 2nd column
2005 Charles Castle Materials Chemicals %change in the 2nd column
2010 Rita Moon Financial Bank %change in the 2nd column
I would like for my new array to give me the first/original row and the cases in which you observe a change. E.g. output:
1997 Charles House Materials Chemicals
1997 Rita Office Financial Bank
2000 Charles Office Materials Chemicals
2003 Rita Star Financial Bank
2005 Charles Castle Materials Chemicals
2010 Rita Moon Financial Bank
My problem is mainly related to the fact that I am dealing with strings. If someone could help me I would appreciate. Thanks a lot for your availability.
0 件のコメント
採用された回答
Cedric
2014 年 5 月 24 日
編集済み: Cedric
2014 年 5 月 24 日
Here one way to proceed:
[~,ia] = unique( arrayfun( @(r)[C{r,2:end}], 1:size(C,1), 'Unif', 0 )) ;
C_chg = C(sort(ia),:)
which outputs
C_chg =
[1997] 'Charles' 'House' 'Materials' 'Chemicals'
[1997] 'Rita' 'Office' 'Financial' 'Bank'
[2000] 'Charles' 'Office' 'Materials' 'Chemicals'
[2003] 'Rita' 'Star' 'Financial' 'Bank'
[2005] 'Charles' 'Castle' 'Materials' 'Chemicals'
[2010] 'Rita' 'Moon' 'Financial' 'Bank'
when applied to your sample.
2 件のコメント
Cedric
2014 年 5 月 24 日
編集済み: Cedric
2014 年 5 月 24 日
Note that this solution assumes that there is no case where an entry can be obtained by concatenation of several others in other rows. To illustrate, the two following entries are equivalent for my solution above.
[1997] 'Charles' 'A' 'AAA' 'AA'
[2000] 'Charles' 'AAA' 'AA' 'A'
I don't beleive that this case can happen, but if you want to avoid this issue, use
[~,ia] = unique( arrayfun( @(r)sprintf('%s ',C{r,2:end}), ...
1:size(C,1), 'Unif', 0 )) ;
instead of the first line.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!