开发者

Vectorising a function array in Matlab

My generic pro开发者_开发知识库blem is illustrated by the following example:

f=@(x,y) cos(x.*y);
Yvalues = linspace(0,1,50);
W = @(x) f(x,Yvalues);

which works fine if I only want to evaluate W at one point at a time. For instance:

norm(W(pi/3)-f(pi/3,Yvalues))
ans =

      0

But how do I go about evaluating W at any number of points?

Thanks in advance.


If you change

f=@(x,y) cos(x.*y);

to

f=@(x,y) cos(x'*y);

you can execute W([1 2 3])

For example,

>> f = @(x,y) cos(x'*y);
>> yv = linspace(0,1,5);
>> W = @(x) f(x,yv);
>> W(1)
ans =
    1.0000    0.9689    0.8776    0.7317    0.5403
>> W(2)
ans =
    1.0000    0.8776    0.5403    0.0707   -0.4161
>> W(3)
ans =
    1.0000    0.7317    0.0707   -0.6282   -0.9900
>> W([1 2 3])
ans =
    1.0000    0.9689    0.8776    0.7317    0.5403
    1.0000    0.8776    0.5403    0.0707   -0.4161
    1.0000    0.7317    0.0707   -0.6282   -0.9900
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜