From: forkandwait w on
If there is a function with a variable number of parameters, is there a way to expand a vector or matrix to fill them each by each? In python, e.g., one can put a "*" in front of a list and it will expand to fill the positional parameters. I would especially like to expand a struct or a cell array this way , in order to put an arbitrary number of vectors into cartprod.

Here is a wishful example:

vecs.a = [ 1 2 3 ]
vecs.b = [4 5]
vecs.c = [7 8 9 10 11]
cartprod(*vecs) == cartprod(vecs.a, vecs.b, vecs.c)

Thx!
From: Walter Roberson on
forkandwait w wrote:
> If there is a function with a variable number of parameters, is there a
> way to expand a vector or matrix to fill them each by each?

If you mean variable number of outputs, then:

[varargout{:}] = cartprod(vecs.a, vecs.b, vecs.c)

Generally speaking, if you have a cell array, then TheCell{:} expands to a
comma-separated list... e.g.,

vecs = {[1 2 3] [4 5] [7 8 9 10 11]}

cartprod(vecs{:})

is the same as cartprod(vecs{1},vecs{2},vecs{3})

You will often find the syntax in use in [] on the left hand side of an
assignment statement (because matlab wants a [] list of variables before the
'='), and you will also often find it in use with vertcat() or horzcat() or as
a trailing parameter to a function call.
From: forkandwait w on
Sorry to topquote, but the below is exactly what I was after. Thanks!

Walter Roberson <roberson(a)hushmail.com> wrote in message <hlupit$ns2$1(a)canopus.cc.umanitoba.ca>...
> forkandwait w wrote:
> > If there is a function with a variable number of parameters, is there a
> > way to expand a vector or matrix to fill them each by each?
>
> If you mean variable number of outputs, then:
>
> [varargout{:}] = cartprod(vecs.a, vecs.b, vecs.c)
>
> Generally speaking, if you have a cell array, then TheCell{:} expands to a
> comma-separated list... e.g.,
>
> vecs = {[1 2 3] [4 5] [7 8 9 10 11]}
>
> cartprod(vecs{:})
>
> is the same as cartprod(vecs{1},vecs{2},vecs{3})
>
> You will often find the syntax in use in [] on the left hand side of an
> assignment statement (because matlab wants a [] list of variables before the
> '='), and you will also often find it in use with vertcat() or horzcat() or as
> a trailing parameter to a function call.