在NumPy中相当于MATLAB的repmat [英] What is the equivalent of MATLAB's repmat in NumPy
问题描述
我想使用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?
推荐答案
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
Python:
In [46]: a = np.array([[1],[1]])
In [47]: np.tile(a, [1,1,1])
Out[47]:
array([[[1],
[1]]])
这篇关于在NumPy中相当于MATLAB的repmat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!