python中是否存在与MATLAB函数bsxfun等价的函数?

时间:2022-07-24 21:24:06

I'm trying to port some of my code from matlab to python, and some of it uses the bsxfun() function for virtual replication followed by multiplication or division (I also use it for logical operations). I'd like to be able to do this without actually replicating the vector (either with a function or with some kind of diagonal matrix) before multiplying or dividing to save on memory and time.

我正在尝试将一些代码从matlab移植到python,其中一些代码使用bsxfun()函数进行虚拟复制,然后进行乘法或除法(我也将它用于逻辑操作)。我想在相乘或除之前不复制向量(用一个函数或某种对角矩阵)以节省内存和时间。

If there's an equivalent of bsxfun in a C library of some kind, that would of course also work.

如果在某种C库中有一个等价的bsxfun,那当然也可以。

2 个解决方案

#1


3  

There isn't really an equivalent of bsxfun, that I'm aware of, although numpy does take care of a lot of broadcasting for you, as others mentioned.

据我所知,bsxfun并没有真正的等价物,尽管numpy确实为你做了很多广播,就像其他人提到的那样。

This is commonly touted as an advantage of numpy over matlab, and it is true that a lot of broadcasting is simpler in numpy, but bsxfun is actually more general, because it can take user-defined functions.

这通常被吹捧为numpy比matlab的优势,而且在numpy中很多广播都更简单,但是bsxfun实际上更通用,因为它可以使用用户定义的函数。

Numpy has this: http://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html but only for 1d.

Numpy有:http://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html,但只适用于1d。

#2


2  

Python is very easy to use compared to matlab bsxfun(x) in python numpy can be easily done by ... in array[], e.g. m[...,:] You can try this:

与matlab bsxfun(x)相比,Python很容易使用。在[]数组,例如m[…你可以试试这个:

>>>m = np.zeros([5,13], dtype=np.float32)
>>>print(m)

    [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

>>>c=np.array([[1,2,3,4,5,6,7,8,9,10,11,12,13]])
>>>print(m[...,:] +4*c)
[[  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]]

#1


3  

There isn't really an equivalent of bsxfun, that I'm aware of, although numpy does take care of a lot of broadcasting for you, as others mentioned.

据我所知,bsxfun并没有真正的等价物,尽管numpy确实为你做了很多广播,就像其他人提到的那样。

This is commonly touted as an advantage of numpy over matlab, and it is true that a lot of broadcasting is simpler in numpy, but bsxfun is actually more general, because it can take user-defined functions.

这通常被吹捧为numpy比matlab的优势,而且在numpy中很多广播都更简单,但是bsxfun实际上更通用,因为它可以使用用户定义的函数。

Numpy has this: http://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html but only for 1d.

Numpy有:http://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html,但只适用于1d。

#2


2  

Python is very easy to use compared to matlab bsxfun(x) in python numpy can be easily done by ... in array[], e.g. m[...,:] You can try this:

与matlab bsxfun(x)相比,Python很容易使用。在[]数组,例如m[…你可以试试这个:

>>>m = np.zeros([5,13], dtype=np.float32)
>>>print(m)

    [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

>>>c=np.array([[1,2,3,4,5,6,7,8,9,10,11,12,13]])
>>>print(m[...,:] +4*c)
[[  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.  40.  44.  48.  52.]]