从给定的numpy的阵列创建块对角阵numpy的 [英] Create block diagonal numpy array from a given numpy array

查看:1459
本文介绍了从给定的numpy的阵列创建块对角阵numpy的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相同数量的行和列的二维numpy的阵列。我想安排他们到具有对角线上的较小的一个更大的阵列。它应该有可能以指定的起始基质多久是对对角线。例如:

I have a 2-dimensional numpy array with an equal number of columns and rows. I would like to arrange them into a bigger array having the smaller ones on the diagonal. It should be possible to specify how often the starting matrix should be on the diagonal. For example:

a = numpy.array([[5, 7], 
                 [6, 3]])

所以,如果我想要这个阵列对角线上的期望的输出的2倍是:

So if I wanted this array 2 times on the diagonal the desired output would be:

array([[5, 7, 0, 0], 
       [6, 3, 0, 0], 
       [0, 0, 5, 7], 
       [0, 0, 6, 3]])

3次:

array([[5, 7, 0, 0, 0, 0], 
       [6, 3, 0, 0, 0, 0], 
       [0, 0, 5, 7, 0, 0], 
       [0, 0, 6, 3, 0, 0],
       [0, 0, 0, 0, 5, 7],
       [0, 0, 0, 0, 6, 3]])

是否与numpy的方法和起始阵列的任意大小来实现这样的快速方法(仍然在考虑起始阵列具有相同数量的行和列)?

Is there a fast way to implement this with numpy methods and for arbitrary sizes of the starting array (still considering the starting array to have the same number of rows and columns)?

推荐答案

的<一个经典案例href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.kron.html\"><$c$c>numpy.kron -

np.kron(np.eye(n), a)

样运行 -

In [57]: n = 2

In [58]: np.kron(np.eye(n), a)
Out[58]: 
array([[ 5.,  7.,  0.,  0.],
       [ 6.,  3.,  0.,  0.],
       [ 0.,  0.,  5.,  7.],
       [ 0.,  0.,  6.,  3.]])

In [59]: n = 3

In [60]: np.kron(np.eye(n), a)
Out[60]: 
array([[ 5.,  7.,  0.,  0.,  0.,  0.],
       [ 6.,  3.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  5.,  7.,  0.,  0.],
       [ 0.,  0.,  6.,  3.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  5.,  7.],
       [ 0.,  0.,  0.,  0.,  6.,  3.]])

这篇关于从给定的numpy的阵列创建块对角阵numpy的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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