Find unique combinations of double and character elements

조회 수: 2 (최근 30일)
Maria
Maria 2014년 8월 10일
답변: Yu Jiang 2014년 8월 10일
I have a cell type variable with 3 columns and 500000 rows, here is an example:
c1 c2 c3
A={1994 'AACE' 2071
1994 'AACE' 30677
1994 'AAC' 1541
1994 'AAC' 2027
1994 'AAC' 7843}
I am applying the following code:
%Unique combinations of c1 and c2
[y,~,ind] = unique([num2str(c1) c2], 'rows'); %Problem
%Count occurences
c = accumarray(ind,1);
%Final variable
F = [y c];
But it gives me an error in the first row, because I am trying to find unique combinations of 'double' and 'char' elements. Can someone help me? Thanks
  댓글 수: 2
Yu Jiang
Yu Jiang 2014년 8월 10일
Hi Maria
It would be easier to help you if you could show the exact error message.
-Yu
Maria
Maria 2014년 8월 10일
Hi Yu,
it gives this error: Error using horzcat Dimensions of matrices being concatenated are not consistent.

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 10일
편집: Azzi Abdelmalek 2014년 8월 10일
A={1994 'AACE' 2071
1994 'AACE' 30677
1994 'AAC' 1541
1994 'AAC' 2027
1994 'AAC' 7843};
c12=cellfun(@(x,y) [num2str(x) y],A(:,1),A(:,2),'un',0);
[y,ii,ind] = unique(c12) %Problem
c = accumarray(ind,1);
F = [A(ii,:) num2cell(c)]

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2014년 8월 10일
[y0,~,ii] = unique(A(:,2));
[y,~,i0] = unique([cell2mat(A(:,1)),ii],'rows')
F = [num2cell(y(:,1)),y0,num2cell(accumarray(i0,1))];
  댓글 수: 1
Maria
Maria 2014년 8월 10일
It gives the following error: Error using horzcat Dimensions of matrices being concatenated are not consistent.

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


Yu Jiang
Yu Jiang 2014년 8월 10일
I believe both the answers by Andrei Bobrov and Azzi Abdelmalek are correct. The error message should be caused by your original definition of A. If you create A exactly as Azzi does, then there should be no problem.
By the way, they both use the function cellfun, which is very convenient for cell operations. More details can be found here (Documentation) .

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by