1

Suppose I have a pre-built Matlab function

function A=f(varagin)
...
end

I call f.m in a file main.m.

When calling f.m, the inputs to include depend on the number of columns of two matrices X and Y.

For example, if

d=2;
X=randn(4,d);
Y=randn(4,d);

then

A=f(X(:,1),X(:,2),Y(:,1),Y(:,2));

If

d=3;
X=randn(4,d);
Y=randn(4,d);

then

A=f(X(:,1),X(:,2),X(:,3),Y(:,1),Y(:,2),Y(:,3));

If

d=4;
X=randn(4,d);
Y=randn(4,d);

then

A=f(X(:,1),X(:,2),X(:,3),X(:,4),Y(:,1),Y(:,2),Y(:,3),Y(:,4));

Could you help me to generalise the calling of f.m in main.m with any d?

TEX
  • 2,249
  • 20
  • 43

1 Answers1

4

The answer is more or less identical to that of your previous question about indexing:

args = {arg1, arg2, arg3};
f(args{:});

args{:} generates a comma-separated list of its elements, which is thus equivalent to using each of its elements as an argument to the function.

To convert the columns of a numeric matrix to a cell array, use mat2cell.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120