在numpy中将3个一维数组连接在一起 [英] Concatenate 3 unidimensional arrays together in numpy

查看:485
本文介绍了在numpy中将3个一维数组连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要离开MatLab进行numpy了,总的来说还可以,但是我梦a以求的是找到一种不错的pythonic方法来完成MatLab要做的事情:

I'm leaving MatLab for numpy and in general it's going ok, but I'm having a nightmare finding a nice pythonic way to do what would have done this in MatLab:

A=[1.0;2.0;3.0;4.0] %Column vector
B=[5.0;6.0;7.0;8.0] %Another one
C=[A,B,B] %4 x 3 matrix

在Python中,像这样设置A:

In Python, setting up A like so:

A=np.array([1,2,3,4])
B=np.array([5,6,7,8])

并进行连接:

C=np.concatenate((A,B,B),axis=1)

将它们一个接一个地堆叠,并且_C,hstack等也会失败.我猜想我需要一种将(4,)numpy数组转换为(4,1)数组的好方法.在我的代码中,这些向量远大于此并且是动态创建的,所以我不能只输入:

Stacks them one on top of the other, and _C, hstack etc fail as well. I'm guessing I need a nice pyythonic way of turning a (4,) numpy array into a (4,1) array. In my code these vectors are much bigger than this and are created dynamically so I can't just type:

A=np.array([[1],[2],[3],[4]])

在此先感谢您的帮助!

推荐答案

您可以使用np.c_[A,B,B],它给出了

array([[1, 5, 5],
       [2, 6, 6],
       [3, 7, 7],
       [4, 8, 8]])

这篇关于在numpy中将3个一维数组连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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