Matrix of functions of two variables
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
James Kristoff
2013-5-9
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?
采纳的回答
James Kristoff
2013-5-9
编辑:James Kristoff
2013-5-9
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.)
更多回答(1 个)
Sean de Wolski
2013-5-9
F = bsxfun(@times,t1,t2)
And for more info:
doc bsxfun
2 个评论
Sean de Wolski
2013-5-9
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...
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!