MATLAB: Matrix of functions of two variables

MATLABmatricesmatrix

I want to generate a matrix whose elements are functions of two variables, say F=f(t1).*f(t2), where t1 varies along row and t2 along column. How to construct such a matrix? Thanks.

Best Answer

If the function f(t) is linear with respect to t (e.g. f(t) = t.*4 + log(5)) then you can simply multiply your inputs and pass the result to f:
t1 = 1:3;
t2 = 1:3;
T = t1'*t2; % this creates a matrix T
F = f(T); % or F = f(t1'*t2);
If the function f(t) is non-linear with respect to t (e.g. f(t) = t.^2) then you could do the following:
t1 = 1:3;
t2 = 1:3;
f1 = f(t1);
f2 = f(t2);
F = f1'*f2; % or F = f(t1)'*f(t2);
This should also work for if f(t) is linear.
Both of these methods require the function f(t) to be written to allow for element-wise mathematics (i.e. using .* in place of * and .^ in place of ^ etc.)