Is bsxfun really applied element-wise?

一个人想着一个人 提交于 2019-12-02 06:14:39

问题


Suppose that I have the following function:

function x = printAndKeepX(x, y)
    x
    y
end

and I invoke bsxfun like so:

bsxfun(@printAndKeepX, 1:4, 1);

Were bsxfun really element-by-element, I would expect printAndKeepX to be called 4 times, with the arguments (x, y) being (1, 1), (2, 1), (3, 1) and (4, 1), respectively. But the output shows that it is called just once with (x, y) being ([1 2 3 4], 1):

x =
     1     2     3     4

y =
     1

Why? How can I know what's considered an "element"?

Edit:
The documentation suggests that sometimes the called function can receive two scalars and sometimes a vector/matrix and a scalar. Can I know for sure which of these is going to happen?

I'm interested in both the regular and GPU versions of bsxfun.


回答1:


The documentation also states that:

fun 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.

In your case B is in fact a scalar, so your function is applied on A only once.

The same applies when the input arrays are matrices. For example, consider a case where bsxfun is invoked with a matrix A={ aij } of size m×n and a vector B={ bij } of size m×1. B would be replicated n times along the second dimension, and the function would be called as follows:

function([a11, ..., a1n], b1)
function([a21, ..., a2n], b2)
...
function([am1, ..., amn], bm)

This results in n function calls for vector-scalar inputs rather then mn function calls for pairs of scalars. If the function is vectorized, it may be reflected in a possibly significant performance gain.



来源:https://stackoverflow.com/questions/18079629/is-bsxfun-really-applied-element-wise

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!