MatrixBerryCore
uniquebyfunc.m
Go to the documentation of this file.
1 function [outUnqVec,indRightToLeftVec,indLeftToRightVec]=uniquebyfunc(inpVec,fCompare,algoName)
2 import mxberry.core.ismemberbyfunc;
3 import mxberry.core.throwerror;
4 %
5 DEFAULT_ALGO_NAME='mempreserve';
6 %
7 if nargin<3
8  algoName=DEFAULT_ALGO_NAME;
9  if nargin<2
10  fCompare=@isequaln;
11  end
12 end
13 %
14 switch algoName
15  case 'memhungry'
16  [isThereVec,indLeftToRightVec]=ismemberbyfunc(inpVec,inpVec,fCompare); %#ok<ASGLU>
17  indVec=1:numel(indLeftToRightVec);
18  indVec=indVec.';
19  if isrow(inpVec)
20  indLeftToRightVec=indLeftToRightVec.';
21  end
22  %
23  isInUniq=ismember(indVec,indLeftToRightVec);
24  indRightToLeftVec=find(isInUniq);
25  [isThereVec,indLeftToRightVec]=ismember(indLeftToRightVec,...
26  indRightToLeftVec); %#ok<ASGLU>
27  outUnqVec=inpVec(isInUniq);
28  case 'mempreserve'
29  % more memory preserving
30  outUnqVec=[];
31  indRightToLeftVec=[];
32  indLeftToRightVec=nan(numel(inpVec),1);
33  for iObj=1:numel(inpVec)
34  [isThereVec,indThereVec]=ismemberbyfunc(inpVec(iObj),outUnqVec,fCompare);
35  if ~isThereVec
36  outUnqVec=[outUnqVec; inpVec(iObj)]; %#ok<AGROW>
37  indRightToLeftVec=[indRightToLeftVec; iObj]; %#ok<AGROW>
38  indLeftToRightVec(iObj)=length(outUnqVec);
39  else
40  indLeftToRightVec(iObj)=indThereVec;
41  end
42  end
43  if isrow(inpVec)
44  outUnqVec=outUnqVec.';
45  end
46  otherwise
47  throwerror('wrongInput','algorithm %s is not supported',algoName);
48 end
function ismemberbyfunc(in leftVec, in rightVec, in fCompare)
ISMEMBERBYFUNC - ismember implementation for arrays of any type where an element comparison is perfor...
function ismember(in leftVec, in rightVec, in varargin)
ISMEMBER - ismember implementation for arrays of any type.
function uniquebyfunc(in inpVec, in fCompare, in algoName)
UNIQUEBYFUNC unique for arrays of any type where an element comparison is performed by a specified fu...
function isrow(in inpArray)