How do I get all non-unique occurrences?

조회 수: 72 (최근 30일)
Dominik Mattioli
Dominik Mattioli 2017년 2월 8일
댓글: Stephen23 2017년 2월 8일
MATLAB's unique function only returns the non-unique indices to either the 'first' or 'last' occurrences of unique elements/rows of a vector/matrix. Is there any way to get 'all' occurrences short of creating my own recursive unique function? I plan on using this for very large matrices (i.e. unique(__,'rows')). Below is a minor example of the problem in the context of a vector:
A = [9 9 9 3 3 3 3];
[U1,AiF] = unique(A,'first');
[U2,AiL] = unique(A,'last')
% Returns
U1 = [3 9] % U1 is the same as U2
AiF = [4 1]
AiL = [7 3]
% I want something like this:
Ai_all = {[4 5 6 7],[1 2 3]}

채택된 답변

Stephen23
Stephen23 2017년 2월 8일
편집: Stephen23 2017년 2월 8일
You could use accumarray to collect the corresponding indices:
>> A = [9,9,9,3,3,3,3];
>> [~,~,idx] = unique(A);
>> idy = 1:numel(A);
>> Z = accumarray(idx(:),idy(:),[],@(n){n});
>> Z{:}
ans =
4
5
6
7
ans =
1
2
3
  댓글 수: 2
Dominik Mattioli
Dominik Mattioli 2017년 2월 8일
Nice, no loops! Do you think that this is applicable to finding unique rows?
A = [1,3;2 4;2 4;5 7;1 3];
~~
UArows = [1 3;2 4;5 7];
iUArows = {[1 5];[2 3];[4]}
??
Stephen23
Stephen23 2017년 2월 8일
>> A = [1,3;2 4;2 4;5 7;1 3];
>> [~,~,idx] = unique(A,'rows');
>> idy = 1:size(A,1);
>> Z = accumarray(idx(:),idy(:),[],@(n){n});
>> Z{:}
ans =
5
1
ans =
3
2
ans =
4

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by