matlab - Concatenating all combinations of matrix rows -


i use matlab , need combine 2 2-dimensional matrices, resulting rows combinations of rows input matrices concatenated together.

i tried ndgrid, creates possible combinations. need input rows stay create output.

here example:

i got:

  a= [1 2 3       4 5 6];    b= [7 8       9 10]; 

i need:

needed = [1 2 3 7 8           1 2 3 9 10           4 5 6 7 8           4 5 6 9 10]; 

i prefer without loops if possible

here's adaptation of yuk's answer using find:

[ib, ia] = find(true(size(b, 1), size(a, 1))); needed = [a(ia(:), :), b(ib(:), :)]; 

this should faster using kron , repmat.

benchmark

a = [1 2 3; 4 5 6]; b = [7 8; 9 10];  tic k = 1:1e3     [ib, ia] = find(true(size(b, 1), size(a, 1)));     needed = [a(ia(:), :), b(ib(:), :)]; end toc  tic k = 1:1e3     needed = [kron(a, ones(size(b,1),1)), repmat(b, [size(a, 1), 1])]; end toc 

the results:

elapsed time 0.030021 seconds. elapsed time 0.17028 seconds. 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -