I am a bit new in using Matlab, and I have a question about defining a multivariable function for vector input.
If the function is a single function, say f(t), I know how to make it for vector input. The general way is to use arrayfun after defining a f(t). How about for multivariable function, say f(x,y)? What I want to do is to get two inputs, say [1 2 3] for x and [4 5 6 7] for y (dimension may be different, but both of them are either column vector or row vector) so that I can calculate to give
[f(1,4),f(1,5),f(1,6),f(1,7);
f(2,4),f(2,5),f(2,6),f(2,7);
f(3,4),f(3,5),f(3,6),f(3,7)]
The difficulty is that the vector input for x and y may not be in the same dimension.
I understand it may be difficult to illustrate if I do not have an example of f(x,y). For my use of f(x,y), it may be very complicated to display f(x,y). For simplicity, treat f(x,y) to be x^2+y, and once defined, you cannot change it to x.^2+y for vector inputs.
x^2+yto work for vectors.But is it correct that it's impossible for you to change the function in a way that you can feed the function with two vectors instead of two variables? It's not hard to make a matrix with all combinations of two vectors of different sizes. - Stewie Griffinarrayfun, it is to use good old fashioned loops. I still haven't seen a single case wherearrayfunis faster than loops. But of course,arrayfunmight look cleaner, and it's shorter. - Stewie Griffinarrayfun(otherwise it's not fair...), theforloop is faster. - EBH