Matrix of functions of two variables
6 views (last 30 days)
Show older comments
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.
2 Comments
James Kristoff
on 9 May 2013
Could you please clarify what you are trying to do?
I am not sure if you are trying to make a matrix which contains functions that have two inputs e.g.
[ f1_1(x,y), f1_2(x,y), f1_3(x,y);...
f2_1(x,y), f2_2(x,y), f2_3(x,y);...
f3_1(x,y), f3_2(x,y), f3_3(x,y) ]
or if you have two functions, one for column vectors and one for row vectors, and you would like to multiply the output of these two functions to create a matrix,
or do you have some matrix,
T = [ t1_1, t1_2, t1_3;...
t2_1, t2_2, t2_3;...
t3_1, t3_2, t3_3 ]
and you would like to apply one function to the rows of T and another to the columns of T,
or is it something else?
Accepted Answer
James Kristoff
on 9 May 2013
Edited: James Kristoff
on 9 May 2013
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.)
More Answers (1)
Sean de Wolski
on 9 May 2013
F = bsxfun(@times,t1,t2)
And for more info:
doc bsxfun
2 Comments
Sean de Wolski
on 9 May 2013
if f is a scalar function then use
bsxfun(@f,t1,t2)
or construct f on the fly
bsxfun(@(x,y)f(x).*f(y),t1,t2)
It's really not quite clear what f is otherwise...
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!