Track changes in a cell array with strings

조회 수: 5 (최근 30일)
Maria
Maria 2014년 5월 24일
댓글: Maria 2014년 5월 27일
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.

채택된 답변

Cedric
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
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.
Maria
Maria 2014년 5월 27일
Thank you very much. Your code works perfectly!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Chemistry에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by