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]].
You can use
ndgrid
and thenarrayfun
. But the latter is more or less the same as a loop:To avoid loops or
arrayfun
you would need to defined the functionf
vectorized.