Home > analyzePRF > utilities > calcposition.m

calcposition

PURPOSE ^

function f = calcposition(list,x)

SYNOPSIS ^

function f = calcposition(list,x)

DESCRIPTION ^

 function f = calcposition(list,x)

 <list> is a vector with unique elements (must be positive integers)
 <x> is a vector whose elements are in <list>.
   elements can be in any order and repeats are okay.

 return a vector the same length as <x> with indices relative to <list>.

 example:
 isequal(calcposition([5 3 2 4],[2 2 5]),[3 3 1])

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function f = calcposition(list,x)
0002 
0003 % function f = calcposition(list,x)
0004 %
0005 % <list> is a vector with unique elements (must be positive integers)
0006 % <x> is a vector whose elements are in <list>.
0007 %   elements can be in any order and repeats are okay.
0008 %
0009 % return a vector the same length as <x> with indices relative to <list>.
0010 %
0011 % example:
0012 % isequal(calcposition([5 3 2 4],[2 2 5]),[3 3 1])
0013 
0014 % construct a vector that gives the correct index for X if you extract the Xth element
0015 xrank = NaN*zeros(1,max(list));  % if max(list) is big, this is ouch
0016 xrank(list) = 1:length(list);
0017 
0018 % get the answers
0019 f = xrank(x);
0020 
0021 % sanity check
0022 assert(~any(isnan(f)),'<list> does not subsume <x>');
0023 
0024 
0025 
0026 
0027 % NICER, BUT SLOWER:
0028 % % init
0029 % f = zeros(size(x));
0030 % % do it
0031 % xu = union(x,[]);
0032 % for p=1:length(xu)
0033 %   temp = find(list==xu(p));
0034 %   assert(~isempty(temp),'<list> does not subsume <x>');
0035 %   f(x==xu(p)) = temp;  % POTENTIALLY SLOW.  see commented code below for a faster but less general solution
0036 % end

Generated on Wed 18-Jun-2014 21:47:41 by m2html © 2005