|
"Eyal Fleminger" <efleminge@gmail.com> wrote in message <hqmogi$rui$1@fred.mathworks.com>...
> Hello
>
> Say I have several vectors, e.g.:
>
> a=[1,2]
> b=[3,4]
> c=[5,6]
>
> I am looking for a way to create a matrix, each line of which is one combination of the values in the input vectors. So for the three vectors abov, I would get:
>
> 1 3 5
> 1 3 6
> 1 4 5
> 1 4 6
> 2 3 5
> 2 3 6
> 2 4 5
> 2 4 6
>
> (the order of the combinations doesn't matter).
>
> For two vectors, it's pretty simple to do this with repmat and sort:
>
> [sort(repmat(a(:),length(b),1)),repmat(b(:),length(a),1)]
>
> However, that gets increasingly more complicated when more than two vectors are input. Does anyone have an idea for a simpler way to do this?
[A,B,C] = ndgrid(a,b,c);
X = [A(:),B(:),C(:)];
Roger Stafford
|