Luke Solo
Luke Solo

Reputation: 205

Matlab - Merge two vectors and a matrix with different dimensions

I have two vectors and matrix, for example:

a = [ 1 2 3 4];
b = [6 7 8];
c = [ 600 700 800 900; 
      100 200 300 400; 
      777 888 555 333];

I would like to get a matrix as:

1 6 600
2 6 700
3 6 800
4 6 900
1 7 100
2 7 200
3 7 300
4 7 400
1 8 777
2 8 888
3 8 555
4 8 333

Is it possible to get this matrix without using loops?

Upvotes: 3

Views: 98

Answers (1)

LowPolyCorgi
LowPolyCorgi

Reputation: 5171

Sure, with meshgrid for instance:

[B, A] = meshgrid(b, a);
C = c';
Res = [A(:) B(:) C(:)];

Best,

Upvotes: 3

Related Questions