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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.