如何在python中将向量附加到矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20978757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append a vector to a matrix in python
提问by Hadi
I want to append a vector to a matrix in python. I tried appendor concatenatemethods but I didn't get the answer. I was previously working with Matlab and there I used this:
我想在 python 中将一个向量附加到一个矩阵。我试过append或concatenate方法,但我没有得到答案。我以前使用过 Matlab,在那里我使用了这个:
m = zeros(10, 4) % define my matrix, 10x4
v = ones(10, 1) % my vecto, 10x1
c = [m,v] % so simple! the result is: 10x5 (the vector added as the last column)
How can I do that in python using numpy?
我如何使用 numpy 在 python 中做到这一点?
采纳答案by Joe Kington
You're looking for np.r_and np.c_. (Think "column stack" and "row stack" (which are also functions) but with matlab-style range generations.)
您正在寻找np.r_和np.c_。(想想“列堆栈”和“行堆栈”(它们也是函数),但使用 matlab 样式的范围生成。)
Also see np.concatenate, np.vstack, np.hstack, np.dstack, np.row_stack, np.column_stacketc.
另见np.concatenate,np.vstack,np.hstack,np.dstack,np.row_stack,np.column_stack等。
For example:
例如:
import numpy as np
m = np.zeros((10, 4))
v = np.ones((10, 1))
c = np.c_[m, v]
Yields:
产量:
array([[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.]])
This is also equivalent to np.hstack([m, v])or np.column_stack([m, v])
这也相当于np.hstack([m, v])或np.column_stack([m, v])
If you're not coming from matlab, hstackand column_stackprobably seem much more readable and descriptive. (And they're arguably better in this case for that reason.)
如果您不是来自 matlab,hstack并且column_stack可能看起来更具可读性和描述性。(出于这个原因,他们在这种情况下可以说更好。)
However, np.c_and np.r_have additional functionality that folks coming from matlab tend to expect. For example:
但是,np.c_并np.r_具有来自 matlab 的人们倾向于期望的附加功能。例如:
In [7]: np.r_[1:5, 2]
Out[7]: array([1, 2, 3, 4, 2])
Or:
或者:
In [8]: np.c_[m, 0:10]
Out[8]:
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 2.],
[ 0., 0., 0., 0., 3.],
[ 0., 0., 0., 0., 4.],
[ 0., 0., 0., 0., 5.],
[ 0., 0., 0., 0., 6.],
[ 0., 0., 0., 0., 7.],
[ 0., 0., 0., 0., 8.],
[ 0., 0., 0., 0., 9.]])
At any rate, for matlab folks, it's handy to know about np.r_and np.c_in addition to vstack, hstack, etc.
无论如何,对于MATLAB人,这是很方便的了解np.r_和np.c_除vstack,hstack等等。
回答by dawg
In numpy it is similar:
在 numpy 中它是类似的:
>>> m=np.zeros((10,4))
>>> m
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
>>> v=np.ones((10,1))
>>> v
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
>>> np.c_[m,v]
array([[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 1.]])

