cosmo index unique hdr

function [cell_indices, unique_values]=cosmo_index_unique(values)
% index unique (combinations of) elements
%
% [cell_indices, unique_values]=cosmo_index_unique(values)
%
% Input:
%   values              either:
%                       - cell with K elements, each of which must be
%                         either a vector with M elements or a cell with
%                         M strings (each element in each cell is treated
%                         as a row); or
%                       - MxK matrix
%
% Returns:
%   cell_indices        Ux1 cell, if along the input there are U unique
%                       combinations of values (element-wise). The K-th
%                       element has U_K indices in the range 1:M indicating
%                       the rows in the input have the same value
%   unique_values       either:
%                       - Kx1 cell, each with U elements, containing the
%                         unique combinations of values of the input
%                         [if the input is a cell]; or
%                       - UxK cell, containing the unique rows in the input
%
% Examples:
%     [i,u]=cosmo_index_unique({[3 2 2 2 1],[3 2 3 3 3]});
%     cosmo_disp(i);
%     %|| { [ 5 ]
%     %||   [ 2 ]
%     %||   [ 3
%     %||     4 ]
%     %||   [ 1 ] }
%     cosmo_disp(u);
%     %|| { [ 1    [ 3
%     %||     2      2
%     %||     2      3
%     %||     3 ]    3 ] }
%
%     % the same operation in matrix operation (input is transposed)
%     [i,u]=cosmo_index_unique([3 2 2 2 1;3 2 3 3 3]');
%     cosmo_disp(i);
%     %|| { [ 5 ]
%     %||   [ 2 ]
%     %||   [ 3
%     %||     4 ]
%     %||   [ 1 ] }
%     cosmo_disp(u);
%     %|| [ 1         3
%     %||   2         2
%     %||   2         3
%     %||   3         3 ]
%
%     % it also works if (some of the) input contains cell strings
%     [i,u]=cosmo_index_unique({{'ccc','bb','bb','bb','a'},...
%                                 [4 3 4 4 4]});
%     cosmo_disp(i);
%     %|| { [ 5 ]
%     %||   [ 2 ]
%     %||   [ 3
%     %||     4 ]
%     %||   [ 1 ] }
%     cosmo_disp(u);
%     %|| { { 'a'      [ 4
%     %||     'bb'       3
%     %||     'bb'       4
%     %||     'ccc' }    4 ] }
%
% #   For CoSMoMVPA's copyright information and license terms,   #
% #   see the COPYING file distributed with CoSMoMVPA.           #