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

vb_util_get_overlapping_vector

PURPOSE ^

return overlapping data of two vectors

SYNOPSIS ^

function [newvec] = vb_util_get_overlapping_vector(vec1, vec2)

DESCRIPTION ^

 return overlapping data of two vectors
 [usage]
   [newvec] = vb_util_get_overlapping_vector(vec1, vec2)
 [input]
     vec1 : <required> vector 1 [N x 1] or [1 x N]
     vec2 : <required> vector 2 [N x 1] or [1 x N]
 [output]
   newvec : overlapping vector of two vectors [N x 1]
 [note]
   mode auto-detect - numeric or string - baseline is vec1
 [history]
   2007-07-11 (Sako) initial version
   2008-02-12 (Sako) added mode auto-detect

 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 [newvec] = vb_util_get_overlapping_vector(vec1, vec2)
0002 % return overlapping data of two vectors
0003 % [usage]
0004 %   [newvec] = vb_util_get_overlapping_vector(vec1, vec2)
0005 % [input]
0006 %     vec1 : <required> vector 1 [N x 1] or [1 x N]
0007 %     vec2 : <required> vector 2 [N x 1] or [1 x N]
0008 % [output]
0009 %   newvec : overlapping vector of two vectors [N x 1]
0010 % [note]
0011 %   mode auto-detect - numeric or string - baseline is vec1
0012 % [history]
0013 %   2007-07-11 (Sako) initial version
0014 %   2008-02-12 (Sako) added mode auto-detect
0015 %
0016 % Copyright (C) 2011, ATR All Rights Reserved.
0017 % License : New BSD License(see VBMEG_LICENSE.txt)
0018 
0019 % --- CHECK ARGUMENTS --- %
0020 if ~exist('vec1', 'var'), vec1 = []; end
0021 if ~exist('vec2', 'var'), vec2 = []; end
0022 [vec1, vec2, mode] = inner_check_arguments(vec1, vec2);
0023 
0024 % --- MAIN PROCEDURE --------------------------------------------------------- %
0025 %
0026 % vec1 and vec2 is [1 x N]
0027 newvec = [];
0028 
0029 if mode == 1
0030   % numeric vector
0031   for iv = vec1
0032     if any(iv == vec2)
0033       newvec = [newvec; iv];
0034     end
0035   end
0036 else
0037   % string vector
0038   n_vec1 = length(vec1);
0039   n_vec2 = length(vec2);
0040   for i_vec1 = 1:n_vec1
0041     for i_vec2 = 1:n_vec2
0042       if strcmp(vec1(i_vec1),vec2(i_vec2))
0043         newvec = [newvec; vec1(i_vec1)];
0044         break;
0045       end
0046     end
0047   end
0048 end
0049 return;
0050 %
0051 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0052 
0053 % --- INNER FUNCTIONS -------------------------------------------------------- %
0054 %
0055 % --- inner_check_arguments()
0056 %
0057 function [vec1, vec2, mode] = inner_check_arguments(vec1, vec2)
0058 func_ = mfilename;
0059 
0060 if isempty(vec1)
0061   error('(%s)vec1 is a required parameter', func_);
0062 end
0063 
0064 if isempty(vec2)
0065   error('(%s)vec2 is a required parameter', func_);
0066 end
0067 
0068 % mode check
0069 if isnumeric(vec1)
0070   mode = 1; % numeric vector
0071   if ~isnumeric(vec2)
0072     % try to convert
0073     tmp_vec2 = vec2;
0074     n_len = length(vec2);
0075     vec2 = zeros(n_len, 1);
0076     for i_data = 1:n_len
0077       vec2(i_data) = str2double(tmp_vec2(i_data));
0078     end
0079   end
0080 else
0081   mode = 2; % string vector
0082   if isnumeric(vec2)
0083     % try to convert
0084     tmp_vec2 = vec2;
0085     n_len = length(vec2);
0086     vec2 = cell(n_len, 1);
0087     for i_data = 1:n_len
0088       vec2(i_data) = {num2str(tmp_vec2(i_data))};
0089     end
0090   end
0091 end
0092 
0093 vec1 = vb_util_arrange_list(vec1,1);
0094 vec2 = vb_util_arrange_list(vec2,1);
0095 
0096 return;
0097 %
0098 % --- end of inner_check_arguments()
0099 %
0100 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0101 
0102 %%% END OF FILE %%%

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