All Permutations of Multivariable Functions in Octave/Matlab

240 Views Asked by At

I am trying to create a matrix of all possible permutations of a function from existing operators.
For example, if there were a function with two variables

f(x,y) with x = [1 2]; y=[1 2];  

I want to make a matrix

[f(1,1),f(1,2);f(2,1),f(2,2)]. 

Would it be possible without using silly loops?
Ideally with an operation similar to f([1:2],[1:2]); which doesn't work.

If you know Haskell, I am looking for the same operation as

[f(x,y)|x<-[1,2],y<-[1,2]].
1

There are 1 best solutions below

0
On

You can use ndgrid and then arrayfun. But the latter is more or less the same as a loop:

[xx, yy] = ndgrid(x, y);
result = arrayfun(f, xx, yy)

To avoid loops or arrayfun you would need to defined the function f vectorized.