Home > atmlab > handy > intersect_unsorted.m

intersect_unsorted

PURPOSE ^

intersect_unsorted intersect two cell arrays, don't sort

SYNOPSIS ^

function C = intersect_unsorted(A, B)

DESCRIPTION ^

 intersect_unsorted intersect two cell arrays, don't sort

 Like Matlab's 'intersect', but does not sort the results.
 Designed and tested for cell arrays of strings with no duplicates.
 Should be much slower than Matlab's.
 Result will keep order of first cell array of strings input.

 FORMAT

   C = intersect(A, B)

 IN

   A   cell array of strings
   B   cell array of strings

 OUT

   C   cell arary of strings containing only those elements of A occuring
       in B

 $Id: intersect_unsorted.m 7368 2012-02-23 14:20:01Z gerrit $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

intersect_unsorted.m

SOURCE CODE ^

0001 function C = intersect_unsorted(A, B)
0002 % intersect_unsorted intersect two cell arrays, don't sort
0003 %
0004 % Like Matlab's 'intersect', but does not sort the results.
0005 % Designed and tested for cell arrays of strings with no duplicates.
0006 % Should be much slower than Matlab's.
0007 % Result will keep order of first cell array of strings input.
0008 %
0009 % FORMAT
0010 %
0011 %   C = intersect(A, B)
0012 %
0013 % IN
0014 %
0015 %   A   cell array of strings
0016 %   B   cell array of strings
0017 %
0018 % OUT
0019 %
0020 %   C   cell arary of strings containing only those elements of A occuring
0021 %       in B
0022 %
0023 % $Id: intersect_unsorted.m 7368 2012-02-23 14:20:01Z gerrit $
0024 
0025 n_A = length(A);
0026 n_B = length(B);
0027 if n_A*n_B==0
0028     C = {};
0029     return
0030 end
0031 
0032 k = 0;
0033 for i = 1:n_A
0034     for j = 1:n_B
0035         if strcmp(A{i}, B{j})
0036             k = k + 1;
0037             C{k} = A{i}; %#ok<AGROW> % cell array permitted to grow
0038         end
0039     end
0040 end
0041 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005