I have a function f
which should give me a matrix where x
is subtracted element wise from m
m = ones(4);
f = @(x) m-x;
when I call this function with
f(5)
everything works fine.
But how can I call this function with a vector
f([5,7])
so I get
-4 -4 -4 -4
-4 -4 -4 -4
-4 -4 -4 -4
-4 -4 -4 -4
and
-6 -6 -6 -6
-6 -6 -6 -6
-6 -6 -6 -6
-6 -6 -6 -6
in something like a 3-dimensional matrix.
If possible I'm searching for the most efficient way to do this, so I do not want to use loops.
Thank you for your help!
You don't need to define a function for that. Just shift the vector to the third dimension and use
bsxfun
: