Python:调整现有数组的大小并用零填充
我认为我的问题应该很简单,但我找不到任何帮助 在互联网上的任何内容。我对 Python 很陌生,所以有可能 我错过了一些非常明显的东西。
我有一个数组 S,就像这样 [xxx](一维)
。我现在创建一个 对角矩阵,sigma
,带有 np.diag(S)
- 到目前为止,一切顺利。现在,我想要 调整这个新的对角数组的大小,以便我可以将它乘以另一个数组 我有。
import numpy as np
...
shape = np.shape((6, 6)) #This will be some pre-determined size
sigma = np.diag(S) #diagonalise the matrix - this works
my_sigma = sigma.resize(shape) #Resize the matrix and fill with zeros - returns "None" - why?
但是,当我打印 my_sigma
的内容时,我得到“None”
。有人可以吗 为我指明正确的方向,因为我无法想象这应该是 这么复杂。
预先感谢您的任何帮助!
Casper
图解:
我有这个:
[x x x]
我想要这个:
[x 0 0]
[0 x 0]
[0 0 x]
[0 0 0]
[0 0 0]
[0 0 0] - or some similar size, but the diagonal elements are important.
I think that my issue should be really simple, yet I can not find any help
on the Internet whatsoever. I am very new to Python, so it is possible that
I am missing something very obvious.
I have an array, S, like this [x x x] (one-dimensional)
. I now create a
diagonal matrix, sigma
, with np.diag(S)
- so far, so good. Now, I want to
resize this new diagonal array so that I can multiply it by another array that
I have.
import numpy as np
...
shape = np.shape((6, 6)) #This will be some pre-determined size
sigma = np.diag(S) #diagonalise the matrix - this works
my_sigma = sigma.resize(shape) #Resize the matrix and fill with zeros - returns "None" - why?
However, when I print the contents of my_sigma
, I get "None"
. Can someone please
point me in the right direction, because I can not imagine that this should be
so complicated.
Thanks in advance for any help!
Casper
Graphical:
I have this:
[x x x]
I want this:
[x 0 0]
[0 x 0]
[0 0 x]
[0 0 0]
[0 0 0]
[0 0 0] - or some similar size, but the diagonal elements are important.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
1.7.0 版本中有一个新的 numpy 函数
numpy .pad
可以在一行中完成此操作。与其他答案一样,您可以使用构造对角矩阵np.diag
在填充之前。此答案中使用的元组
((0,N),(0,0))
指示要填充的矩阵的“边”。B
现在等于:There is a new numpy function in version 1.7.0
numpy.pad
that can do this in one-line. Like the other answers, you can construct the diagonal matrix withnp.diag
before the padding.The tuple
((0,N),(0,0))
used in this answer indicates the "side" of the matrix which to pad.B
is now equal to:sigma.resize()
返回None
因为它就地操作。np.resize(sigma, shape)
,另一方面,返回结果,但不是用零填充,而是用数组的重复项填充。此外,
shape()
函数返回输入的形状。如果您只想预定义一个形状,只需使用元组即可。但是,这将首先展平原始数组,然后将其重建为给定的形状,从而破坏原始顺序。如果您只想用零“填充”,则可以直接索引到生成的零矩阵,而不是使用 resize() 。
sigma.resize()
returnsNone
because it operates in-place.np.resize(sigma, shape)
, on the other hand, returns the result but instead of padding with zeros, it pads with repeats of the array.Also, the
shape()
function returns the shape of the input. If you just want to predefine a shape, just use a tuple.However, this will first flatten out your original array, and then reconstruct it into the given shape, destroying the original ordering. If you just want to "pad" with zeros, instead of using
resize()
you can just directly index into a generated zero-matrix.我看到编辑...您必须先创建零,然后将一些数字移入其中。
np.diag_indices_from
可能对您有用I see the edit... you do have to create the zeros first and then move some numbers into it.
np.diag_indices_from
might be useful for you此解决方案与
resize
函数配合使用获取示例数组
此dosent工作,它只是添加一个重复值
此确实工作
This solution works with
resize
functionTake a sample array
This dosent work, it just add a repeating values
This does work
另一个纯Python解决方案是
b,现在
它是一个可怕的解决方案,我承认这一点。
不过,它说明了一些可以使用的
list
类型的函数。Another pure python solution is
b
is nowit's a hideous solution, I'll admit that.
However, it illustrates some functions of the
list
type that can be used.