How do you bring about all possible combinations from two row vectors?

Say for example I have
x=[1 2 3 4] y=[1 2 3 4]
and I wanted to create a new vector z which would give all combinations of the elements of x and y. It would look something like
z=[(1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) etc etc]. Is this even possible?

 Réponse acceptée

Matt J
Matt J le 21 Fév 2013
Modifié(e) : Matt J le 21 Fév 2013
Yes. Use NDGRID or MESHGRID, e.g.,
>> [x,y]=ndgrid([1,2], [3,4])
x =
1 1
2 2
y =
3 4
3 4
>> z=[x(:),y(:)]
z =
1 3
2 3
1 4
2 4

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by