Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unique values of a vector with same order as in the vector in matlab

I have a vector A=[2,5,6,2,4,13,34,3,34]. I want to find a unique value of this vector but not in sorted order! I searched in Matlab site and I found this function

[C, ia, ic] = unique(A,'rows','stable')

but this function is not recognized in Matlab R2011a ! probably this function works on version higher than 2011 !! anybody knows how can I find the unique values of A with the same order as in A like : A=[2,5,6,4,13,34,3]

like image 508
Reza_M Avatar asked Dec 25 '22 22:12

Reza_M


1 Answers

Here's an implementation if you are working with 2D arrays and would like to get the same functionality as unique(A,'rows','stable') -

function [C, ia, ic] = unique_rows_stable(A)

[unqmat_notinorder,row_ind,labels] = unique(A,'rows','first');

[ia,ordered_ind] = sort(row_ind);

C = unqmat_notinorder(ordered_ind,:);

[~,ic] = ismember(labels,ordered_ind);
%// Or [ic,~] = find(bsxfun(@eq,ordered_ind,labels'))

return;
like image 53
Divakar Avatar answered May 02 '23 19:05

Divakar