bsxfun不能按我期望的那样在恒定函数上工作 [英] bsxfun doesn't work as I expect on a constant function

查看:89
本文介绍了bsxfun不能按我期望的那样在恒定函数上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab R2016a中,我有一大套成对的小X向量和Y向量(例如10,000个1x3 X向量与10,000个1x3 Y向量配对).对于每个{X,Y}对,我想为X和Y中元素的每个成对组合计算一个2标量参数函数(因此在我的示例中,我将获得10,000个3x3矩阵).

In Matlab R2016a, I have a large set of small X-vectors and Y-vectors which are paired (e.g. 10,000 1x3 X-vectors paired with 10,000 1x3 Y vectors). For each {X,Y} pair, I want to calculate a 2-scalar-argument function for every pairwise combination of the elements in X and Y, (so in my example I would get 10,000 3x3 matrices).

我以为我可以使用bsxfun来执行这些计算,但是当我尝试进行一些简单的测试时,它不起作用. bsxfun(@(x,y) x*y,[1 2],[1 2]')返回:

I thought I could use bsxfun to perform these calculations, but it doesn't work when I try to do some simple tests. bsxfun(@(x,y) x*y,[1 2],[1 2]') returns:

ans =

     1     2
     2     4

这是我期望的.但是,bsxfun(@(x,y) 1,[1 2],[1 2]')返回:

Which is what I would expect. However, bsxfun(@(x,y) 1,[1 2],[1 2]') returns:

Error using bsxfun
Specified function handle produces invalid output dimensions. The function handle
must be a binary elementwise function.

这毫无意义.函数句柄一个二进制的逐元素函数,总是返回标量1,因此bsxfun应该给出与ones(2,2)相同的结果,除非我不了解bsxfun的工作原理.

Which makes no sense. The function handle is a binary elementwise function that always returns the scalar 1, so bsxfun should give the same result as ones(2,2), unless I'm not understanding how bsxfun works.

推荐答案

传递给bsxfun 的函数句柄输入不是标量.在R2016b之前的版本中,输入为 标量或相同大小.

The inputs to the function handle that are passed to bsxfun are not scalars. In versions prior to R2016b, the inputs are either scalar or they are the same size.

FUNC也可以是未列出的任何二进制按元素函数的句柄 以上. C = FUNC(A,B)形式的二进制按元素函数 接受任意大小但相等的数组AB,并返回输出 相同的大小.输出数组C中的每个元素都是结果 仅对AB的相应元素执行的操作. FUNC必须 还支持标量扩展,例如,如果AB是标量,则C是标量扩展 将标量应用于另一个输入数组中的每个元素的结果.

FUNC can also be a handle to any binary element-wise function not listed above. A binary element-wise function in the form of C = FUNC(A,B) accepts arrays A and B of arbitrary but equal size and returns output of the same size. Each element in the output array C is the result of an operation on the corresponding elements of A and B only. FUNC must also support scalar expansion, such that if A or B is a scalar, C is the result of applying the scalar to every element in the other input array.

在自R2016b开始的版本中,它们不必大小相等,而应为

In releases since R2016b, they do not have to be equal sizes, but should be compatible sizes

在您显示的示例中,函数句柄的第一个输入是标量,第二个是向量(y),并且对x的每个元素求值,并且输出应为大小的y

In the example you have shown, the first input to the function handle is a scalar and the second is a vector (y) and the function is evaluated for every element of x and the output is expected to be the size of y

在您发布的情况下,对bsxfun的调用实质上等效于:

In the case you've posted, the call to bsxfun is essentially the equivalent of:

x = [1 2];
y = [1 2].';

yourfunc = @(x,y)x * y;

for k = 1:numel(x)
    output(:,k) = yourfunc(x(k), y)
end

如果要为每个条目返回一个1,则需要用产生适当大小的输出的东西替换函数.

If you want to return a 1 for every entry, you need to replace your function with something that yields the appropriately sized output.

bsxfun(@(x,y)ones(max(size(x), size(y))), [1 2], [1 2]')

如何构造函数句柄确实取决于您的特定问题

How you formulate the function handle really depends upon your specific problem

这篇关于bsxfun不能按我期望的那样在恒定函数上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆