像在MATLAB中那样在Python中连接矩阵/向量? [英] Concatenate matrices/vectors in Python like in MATLAB?

查看:192
本文介绍了像在MATLAB中那样在Python中连接矩阵/向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Axyz为适当大小的某些矢量或矩阵.然后,在MATLAB中,可以很容易地从其中构建一个超级矩阵" B:

Let A, x, y and z be some vectors or matrices of appropriate size. Then in MATLAB one can build a "super matrix" B out of them very easily:

A = [1 2;3 4];
x = [4;5];
y = [1 2];
z = 4;
B = [A x;y z];

输出为:

>> B

B =

     1     2     4
     3     4     5
     1     2     4

在NumPy中达到相同效果的最佳方法是什么?

What is the best way to achieve the same effect in NumPy?

推荐答案

您可以使用 numpy.block :

You can use numpy.block:

In [27]: a
Out[27]: 
array([[1, 2],
       [3, 4]])

In [28]: x
Out[28]: 
array([[4],
       [5]])

In [29]: y
Out[29]: array([1, 2])

In [30]: z
Out[30]: 4

In [31]: np.block([[a, x], [y, z]])
Out[31]: 
array([[1, 2, 4],
       [3, 4, 5],
       [1, 2, 4]])

这篇关于像在MATLAB中那样在Python中连接矩阵/向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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