Home > vbmeg > functions > common > utility > vb_util_get_index.m

vb_util_get_index

PURPOSE ^

return index lists which are the same in two lists

SYNOPSIS ^

function [idx1, idx2] = vb_util_get_index(list1, list2)

DESCRIPTION ^

 return index lists which are the same in two lists
 [usage]
   [idx1, idx2] = vb_util_get_index(list1, list2)
 [input]
   list1 : <required> 1st list e.g. [1 3 10 11]
         :  [N_list1 x 1] or [1 x N_list1]
   list2 : <required> 2nd list e.g. [1 2 3 ....]
         :  [N_list2 x 1] or [1 x N_list2]
 [output]
    idx1 : the same values index list in list1
         :  [N_samelist x 1]
    idx2 : the same values index list in list2
         :  [N_samelist x 1]
 [note]
   (prior condition)
     it is not necessary for either of two lists to include another one
     if they are string lists, they must be cell arrays.
 [history]
   2008-02-19 (Sako) initial version
   2010-01-22 (Sako) modified to return empty when whichever list1 or list2
                     is empty

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [idx1, idx2] = vb_util_get_index(list1, list2)
0002 % return index lists which are the same in two lists
0003 % [usage]
0004 %   [idx1, idx2] = vb_util_get_index(list1, list2)
0005 % [input]
0006 %   list1 : <required> 1st list e.g. [1 3 10 11]
0007 %         :  [N_list1 x 1] or [1 x N_list1]
0008 %   list2 : <required> 2nd list e.g. [1 2 3 ....]
0009 %         :  [N_list2 x 1] or [1 x N_list2]
0010 % [output]
0011 %    idx1 : the same values index list in list1
0012 %         :  [N_samelist x 1]
0013 %    idx2 : the same values index list in list2
0014 %         :  [N_samelist x 1]
0015 % [note]
0016 %   (prior condition)
0017 %     it is not necessary for either of two lists to include another one
0018 %     if they are string lists, they must be cell arrays.
0019 % [history]
0020 %   2008-02-19 (Sako) initial version
0021 %   2010-01-22 (Sako) modified to return empty when whichever list1 or list2
0022 %                     is empty
0023 %
0024 % Copyright (C) 2011, ATR All Rights Reserved.
0025 % License : New BSD License(see VBMEG_LICENSE.txt)
0026 
0027 % --- CHECK ARGUMENTS --- %
0028 if ~exist('list1', 'var'), list1 = []; end
0029 if ~exist('list2', 'var'), list2 = []; end
0030 [list1, list2, data_type] = inner_check_arguments(list1, list2);
0031 if isempty(list1) || isempty(list2)
0032   idx1 = [];
0033   idx2 = [];
0034   return;
0035 end
0036 
0037 % --- MAIN PROCEDURE --------------------------------------------------------- %
0038 %
0039 idx1 = [];
0040 idx2 = [];
0041 
0042 switch data_type
0043   case  0 % string
0044     for i = 1:size(list1,1)
0045       tmp_idx = find(strcmp(list2, list1{i}) == 1);
0046       if ~isempty(tmp_idx)
0047         idx2 = [idx2; tmp_idx];
0048         idx1 = [idx1; i];
0049       end
0050     end
0051     
0052   case  1 % numerical
0053     for i = 1:size(list1,1)
0054       tmp_idx = find(list2 == list1(i));
0055       if ~isempty(tmp_idx)
0056         idx2 = [idx2; tmp_idx];
0057         idx1 = [idx1; i];
0058       end
0059     end
0060 end
0061 
0062 return;
0063 %
0064 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0065 
0066 % --- INNER FUNCTIONS -------------------------------------------------------- %
0067 %
0068 % --- inner_check_arguments()
0069 %
0070 function [list1, list2, data_type] = inner_check_arguments(list1, list2)
0071 func_ = mfilename;
0072 data_type = [];
0073 
0074 if isempty(list1)
0075 %   error('(%s)list1 is a required parameter', func_);
0076   return;
0077 end
0078 
0079 if isempty(list2)
0080 %   error('(%s)list2 is a required parameter', func_);
0081   return;
0082 end
0083 
0084 % 1) numeric,  0) string
0085 if isnumeric(list1)
0086   data_type = 1;
0087 else
0088   data_type = 0;
0089 end
0090 
0091 % arrange to [N x 1]
0092 list1 = vb_util_arrange_list(list1);
0093 list2 = vb_util_arrange_list(list2);
0094 return;
0095 %
0096 % --- end of inner_check_arguments()
0097 %
0098 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0099 
0100 %%% END OF FILE %%%

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005