numpy.tile不能作为Matlab repmat使用 [英] numpy.tile did not work as Matlab repmat

查看:66
本文介绍了numpy.tile不能作为Matlab repmat使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据在NumPy中相当于MATLAB的repmat,我尝试使用python从3x3数组构建3x3x5数组.

According to What is the equivalent of MATLAB's repmat in NumPy, I tried to build 3x3x5 array from 3x3 array using python.

在Matlab中,这项工作符合我的预期.

In Matlab, this work as I expected.

a = [1,1,1;1,2,1;1,1,1];
a_= repmat(a,[1,1,5]);

size(a_)= 3 3 5

size(a_) = 3 3 5

但是对于numpy.tile

But for numpy.tile

b = numpy.array([[1,1,1],[1,2,1],[1,1,1]])
b_ = numpy.tile(b, [1,1,5])

b_.shape =(1、3、15)

b_.shape = (1, 3, 15)

如果要生成与Matlab中相同的数组,等效项是什么?

If I want to generate the same array as in Matlab, what is the equivalent?

编辑1

我期望得到的输出是

b_(:,:,1) =

1 1 1  
1 2 1  
1 1 1  

b_(:,:,2) =

1 1 1  
1 2 1  
1 1 1  

b_(:,:,3) =

1 1 1  
1 2 1  
1 1 1  

b_(:,:,4) =  

1 1 1  
1 2 1  
1 1 1  
b_(:,:,5) =

1 1 1  
1 2 1  
1 1 1  

但是@farenorth和 numpy.dstack 给出的是

but what @farenorth and the numpy.dstack give is

[[[1 1 1 1 1]  
[1 1 1 1 1]  
[1 1 1 1 1]]  

[[1 1 1 1 1]  
[2 2 2 2 2]  
[1 1 1 1 1]]  

[[1 1 1 1 1]  
[1 1 1 1 1]  
[1 1 1 1 1]]]  

推荐答案

NumPy函数通常不是matlab函数的直接替代".通常,等效"函数的使用方式之间存在细微的差异.适应确实需要时间,但是我发现过渡非常值得.

NumPy functions are not, in general, 'drop-in' replacements for matlab functions. Often times there are subtle difference to how the 'equivalent' functions are used. It does take time to adapt, but I've found the transition to be very worthwhile.

在这种情况下, np.tile 文档指出了当您尝试将数组平铺到比定义的数组更大的维度时会发生什么情况

In this case, the np.tile documentation indicates what happens when you are trying to tile an array to higher dimensions than it is defined,

numpy.tile(A,代表)

numpy.tile(A, reps)

通过重复多次代表次数来构造数组.

Construct an array by repeating A the number of times given by reps.

如果代表的长度为d,则结果的尺寸将为max(d,A.ndim).

If reps has length d, the result will have dimension of max(d, A.ndim).

如果A.ndim<d,通过添加新轴将A提升为d维.因此,将形状(3,)阵列提升为(1,3)以进行2D复制,或将形状(1、1、3)提升为3D复制.如果这不是理想的行为,请在调用此函数之前手动将A提升为d维.

If A.ndim < d, A is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired behavior, promote A to d-dimensions manually before calling this function.

在这种情况下,将数组转换为 [1、3、3] 的形状,然后进行平铺.因此,要获得所需的行为,只需确保将新的单例维度添加到所需数组即可,

In this case, your array is being cast to a shape of [1, 3, 3], then being tiled. So, to get your desired behavior just be sure to append a new singleton-dimension to the array where you want it,

>>> b_ = numpy.tile(b[..., None], [1, 1, 5])
>>> print(b_.shape)
(3, 3, 5)

请注意,这里我使用了 None (即 np.newaxis )和省略号来在数组末尾指定一个新维度.您可以在此处找到更多信息.

Note here that I've used None (i.e. np.newaxis) and ellipses notation to specify a new dimension at the end of the array. You can find out more about these capabilities here.

另一个受OP的评论启发的选项是:

Another option, which is inspired by the OP's comment would be:

b_ = np.dstack((b, ) * 5)

在这种情况下,我使用了元组乘法来复制"数组,然后由

In this case, I've used tuple multiplication to 'repmat' the array, which is then constructed by np.dstack.

如@hpaulj所示,Matlab和NumPy显示矩阵的方式不同.要复制Matlab输出,您可以执行以下操作:

As @hpaulj indicated, Matlab and NumPy display matrices differently. To replicate the Matlab output you can do something like:

>>> for idx in xrange(b_.shape[2]):
...    print 'b_[:, :, {}] = \n{}\n'.format(idx, str(b_[:, :, idx]))
...
b_[:, :, 0] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

b_[:, :, 1] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

b_[:, :, 2] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

b_[:, :, 3] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

b_[:, :, 4] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

祝你好运!

这篇关于numpy.tile不能作为Matlab repmat使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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