Find the unique values ​​of the vector in the 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 the unique value of this vector, but not in sorted order! I searched 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 feature works on the version above 2011! Does anyone know how I can find unique values ​​of A in the same order as in A, for example: A = [2,5,6,4,13,34,3]

+4
source share
3 answers

Assuming you have a vector (so the version 'rows'doesn't make sense), the solution here is based on bsxfun:

[~, ind] = max(bsxfun(@eq, A, A.'));
ind = ind(ind>=1:numel(ind));
C = A(ind);

: (bsxfun(@eq, A, A.')). ([~, ind]=max(...)). (.. , ), (ind = ind(ind>=...). (C = A(ind)).

+3

, 2D- , 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;
+4
A=[2,5,6,2,4,13,34,3,34];
[B, ia] = sort(A);     % B = A(ia)
ib(ia) = 1:length(B);  % A = B(ib)
[C, ic] = unique(B);   % C = B(ic)
D = B(ib(ic));         % unsorted unique values
+2
source

All Articles