问题描述
我想使用 NumPy 执行以下 MATLAB 代码的等效代码:repmat([1; 1], [1 1 1])
.我将如何做到这一点?
I would like to execute the equivalent of the following MATLAB code using NumPy: repmat([1; 1], [1 1 1])
. How would I accomplish this?
推荐答案
这里有一个更好的(官方)NumPy for Matlab 用户 链接 - 恐怕 mathesaurus 已经过时了.
Here is a much better (official) NumPy for Matlab Users link - I'm afraid the mathesaurus one is quite out of date.
repmat(a, m, n)
的 numpy 等价物是 tile(a, (m, n))
.
The numpy equivalent of repmat(a, m, n)
is tile(a, (m, n))
.
这适用于多个维度,并给出与 matlab 类似的结果.(Numpy 提供了您所期望的 3d 输出数组 - matlab 出于某种原因提供了 2d 输出 - 但内容相同).
This works with multiple dimensions and gives a similar result to matlab. (Numpy gives a 3d output array as you would expect - matlab for some reason gives 2d output - but the content is the same).
MATLAB:
>> repmat([1;1],[1,1,1])
ans =
1
1
蟒蛇:
In [46]: a = np.array([[1],[1]])
In [47]: np.tile(a, [1,1,1])
Out[47]:
array([[[1],
[1]]])
这篇关于NumPy 中 MATLAB 的 repmat 的等价物是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!