Home > bml > utils > bml_getidx.m

bml_getidx

PURPOSE ^

BML_GETIDX gets the first indices of the elements in the domain

SYNOPSIS ^

function idx = bml_getidx(element,collection)

DESCRIPTION ^

 BML_GETIDX gets the first indices of the elements in the domain

 Use as
   idx = bml_getidx(element,collection)

 index 0 for elements not found

 Use as
   index = bml_get_index(element,collection)

 elements - array or cell
 domain - collection of elements from where to extract the index

 returns an array of natural indices

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function idx = bml_getidx(element,collection)
0002 
0003 % BML_GETIDX gets the first indices of the elements in the domain
0004 %
0005 % Use as
0006 %   idx = bml_getidx(element,collection)
0007 %
0008 % index 0 for elements not found
0009 %
0010 % Use as
0011 %   index = bml_get_index(element,collection)
0012 %
0013 % elements - array or cell
0014 % domain - collection of elements from where to extract the index
0015 %
0016 % returns an array of natural indices
0017 
0018   if ischar(element)
0019     element = {element};
0020   end
0021 
0022   if iscellstr(element)
0023     assert(iscellstr(collection),'incompatible elements and collection');  
0024     idx = cellfun(@(x) zero_if_empty(find(strcmp(collection,x),1)),element,'UniformOutput',true);
0025   elseif iscell(element)
0026     assert(iscell(collection),'incompatible elements and collection');
0027     idx = cellfun(@(x) zero_if_empty(find(collection==x,1)),element,'UniformOutput',true);  
0028   elseif isnumeric(element)
0029     assert(isnumeric(collection),'incompatible elements and collection');   
0030     idx = arrayfun(@(x) zero_if_empty(find(collection==x,1)),element); 
0031   else 
0032     error('unknown type for element');
0033   end
0034 
0035   if size(idx,1) > size(idx,2)
0036     idx = idx';
0037   end
0038 end
0039 
0040 function y = zero_if_empty(x)
0041   if isempty(x)
0042     y = 0;
0043   else
0044     y = x;
0045   end
0046 end

Generated on Tue 25-Sep-2018 10:08:19 by m2html © 2005