Home > atmlab > math > combine_rows.m

combine_rows

PURPOSE ^

combine_rows Return combinations of rows

SYNOPSIS ^

function M = combine_rows(A, B, varargin)

DESCRIPTION ^

 combine_rows Return combinations of rows

 Returns every combination of rows for all arguments (matrices)

 FORMAT

   M = combine_rows(A, B, C, ...) % any number of arguments

 IN

   ... matrices

 OUT

   M   matrix with each row a unique combination of the rows of A, B, ...

 $Id: combine_rows.m 6555 2010-10-08 14:48:16Z gerrit $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

combine_rows.m

SOURCE CODE ^

0001 function M = combine_rows(A, B, varargin)
0002 
0003 % combine_rows Return combinations of rows
0004 %
0005 % Returns every combination of rows for all arguments (matrices)
0006 %
0007 % FORMAT
0008 %
0009 %   M = combine_rows(A, B, C, ...) % any number of arguments
0010 %
0011 % IN
0012 %
0013 %   ... matrices
0014 %
0015 % OUT
0016 %
0017 %   M   matrix with each row a unique combination of the rows of A, B, ...
0018 %
0019 % $Id: combine_rows.m 6555 2010-10-08 14:48:16Z gerrit $
0020 
0021 switch nargin
0022     case 0
0023         M = [];
0024     case 1
0025         M = A;
0026     case 2
0027         % pre-allocate
0028         M = nan(size(A, 1)*size(B, 1), size(A, 2)+size(B, 2));
0029         
0030         c = 1;
0031         for i = 1:size(A, 1)
0032             for j = 1:size(B, 1)
0033                 M(c, 1:size(A, 2)) = A(i, :);
0034                 M(c, (1+size(A, 2)):end) = B(j, :);
0035                 c = c + 1;
0036             end
0037         end
0038     otherwise % >3; call self where A+B is combined with the rest first
0039         M = combine_rows(...
0040             combine_rows(A, B), ...
0041             combine_rows(varargin{:}));
0042 end

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