matlab常量匿名函数仅返回一个值,而不是数组 [英] matlab constant anonymous function returns only one value instead of an array
问题描述
我已经在网上搜寻了两个早晨,却什么也没发现,希望能对您有所帮助.
I've been searching the net for a couple of mornings and found nothing, hope you can help.
我有这样的匿名功能
f = @(x,y) [sin(2*pi*x).*cos(2*pi*y), cos(2*pi*x).*sin(2*pi*y)];
需要对点数组进行评估,例如
that needs to be evaluated on an array of points, something like
x = 0:0.1:1;
y = 0:0.1:1;
w = f(x',y');
现在,在上面的示例中,一切正常,结果w是一个11x2矩阵,在每一行中都有正确的值f(x(i),y(i)). 当我将函数更改为具有恒定值时,就会出现问题:
Now, in the above example everything works fine, the result w is a 11x2 matrix with in each row the correct value f(x(i), y(i)). The problem comes when I change my function to have constant values:
f = @(x,y) [0, 1];
现在,即使像以前一样使用数组输入,我也只能得到一个1x2数组,例如w = [0,1]; 当然,我希望具有与以前相同的结构,即11x2矩阵.
Now, even with array inputs like before, I only get out a 1x2 array like w = [0,1]; while of course I want to have the same structure as before, i.e. a 11x2 matrix.
我不知道Matlab为什么要这么做...
I have no idea why Matlab is doing this...
编辑1 抱歉,我以为我在原始问题中写的内容很清楚,但是我看到一些人在问,所以这里有一个澄清:我想要的是再次拥有一个11x2矩阵,因为我正在向函数提供数组具有11个元素. 这意味着我希望输出与第一个示例完全相同,只是其中的值发生了变化:一个矩阵,该矩阵包含11行2列,第一列只有0值,第二列只有1值,因为对于所有x(i)和y(i)的答案应该是向量[0,1]. 这意味着我希望拥有:
EDIT 1 Sorry, I thought it was pretty clear from what I wrote in the original question, but I see some of you asking, so here is a clarification: what I want is to have again a 11x2 matrix, since I am feeding the function with arrays with 11 elements. This means I expect to have an output exactly like in the first example, just with changed values in it: a matrix with 11 rows and 2 columns, with only values 0 in the first column and only values 1 in the second, since for all x(i) and y(i) the answer should be the vector [0,1]. It means I expect to have:
w = [0 1
0 1
0 1
...
0 1]
对我来说似乎很自然...
seems pretty natural to me...
推荐答案
您正在定义一个函数f = @(x,y) [0, 1];
,该函数具有输入参数x,y和输出[0,1]
.您还希望发生什么?
You are defining a function f = @(x,y) [0, 1];
which has the input parameters x,y and the output [0,1]
. What else do you expect to happen?
更新:
这应符合您的描述
g=@(x,y)[zeros(size(x)),ones(size(y))]
g(x',y')
这篇关于matlab常量匿名函数仅返回一个值,而不是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!