Benvaulter
Benvaulter

Reputation: 249

Matlab find unique column-combinations in matrix and respective index

I have a large matrix with with multiple rows and a limited (but larger than 1) number of columns containing values between 0 and 9 and would like to find an efficient way to identify unique row-wise combinations and their indices to then build sums (somehwat like a pivot logic). Here is an example of what I am trying to achieve:

a =

 1     2     3
 2     2     3
 3     2     1
 1     2     3
 3     2     1

uniqueCombs =

 1     2     3
 2     2     3
 3     2     1

numOccurrences =

 2
 1
 2

indizies:

[1;4]
[2]
[3;5]

From matrix a, I want to first identify the unique combinations (row-wise), then count the number occurrences / identify the row-index of the respective combination.

I have achieved this through generating strings with num2str and strcat, but this method appears to be very slow. Along these thoughts I have tried to find a way to form a new unique number through concatenating the values horizontally, but Matlab does not seem to support this (e.g. from [1;2;3] build 123). Sums won't work because they would remove the possibility to identify unique combinations. Any suggestions on how to best achieve this? Thanks!

Upvotes: 0

Views: 690

Answers (1)

UJIN
UJIN

Reputation: 1758

To get the unique rows you can use unique with the 'rows' option enabled:

[C, ix, ic] = unique(a, 'rows', 'stable');

C contains the unique rows; ix the indexes of the first occurrences of those rows in C; ic basically contains the information you want. To access it you can loop over the indexes of ix and save them in a cell array:

indexes = cell(1, length(ix));
for k = 1:length(ix)
  indexes{k} = find(ic == ix(k));
end

indexes will be a cell array containing the indexes you were looking for. For example:

indexes{1}

% ans =
%
% 1
% 4

And to count the occurrences of a particular combination you can just use numel. For example:

numel(indexes{1})

% ans = 
%
% 2 

Upvotes: 0

Related Questions