MATLAB: How to get all non-unique occurrences

graphindexingMATLAB

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]}

Best Answer

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