简体   繁体   English

将元素插入到n维数组的任意位置

[英]Inserting element into arbitrary position of an n-dimensional array

I am using Python 2.7 to create a complex-valued (mxn)-dimensional array without an initially known fixed size (ie m and n are not known ahead of time) which will have particular elements assigned different values. 我正在使用Python 2.7创建一个复数值(mxn)维数组,而没有一个最初已知的固定大小(即m和n提前未知),它将为特定元素分配不同的值。 Thus, I will be changing existing elements or adding new elements to this array at arbitrary positions which will be specified in the future. 因此,我将在以后指定的任意位置更改现有元素或向此数组添加新元素。

In general, I would like to transform an initial array of prescribed size into an (mxn)-dimensional array. 通常,我想将指定大小的初始数组转换为(mxn)维数组。 For example, if I start with 例如,如果我开始

[ 0.+0.j  0.+0.j  0.+0.j]
[ 0.+0.j  0.+0.j  0.+0.j]

I would like to update it to yield either case 1, case 2, or case 3 (whichever one I decide to choose). 我想对其进行更新以产生情况1,情况2或情况3(我决定选择哪种情况)。 Essentially, all I want to do is add either a zero row or column (or both) to the initial array. 本质上,我想做的就是向初始数组添加零行或零列(或两者都添加)。

case 1: 情况1:

[ 0.+0.j  0.+0.j  0.+0.j  0.+0.j] 
[ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]

case 2: 情况2:

[ 0.+0.j  0.+0.j  0.+0.j]
[ 0.+0.j  0.+0.j  0.+0.j]
[ 0.+0.j  0.+0.j  0.+0.j]

case 3: 情况3:

[ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
[ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
[ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]

When using a similar approach as above but instead using np.insert, I can reproduce what I want by: 当使用与上述类似的方法但使用np.insert时,我可以通过以下方式重现我想要的内容:

import numpy as np
T = np.zeros((2,3),dtype=np.complex_)
T = np.insert(T,len(T[0]),1,axis = 1)

or 要么

T = np.zeros((2,3),dtype=np.complex_)
T = np.insert(T,len(T[:,0]),1,axis = 0)

Using this method, I can play around to achieve case 1, 2, or 3, but are there any particularly efficient methods? 使用这种方法,我可以尝试实现案例1、2或3,但是有没有特别有效的方法?

Once again, the constraints are that an object with elements of type complex must be used since these entries will be used in further arithmetic. 同样,约束条件是必须使用具有复杂类型元素的对象,因为这些条目将在进一步的算法中使用。 I could separate the real and complex part of the elements and create two more lists, but mathematical operations would still need to be applied to them. 我可以将元素的实际部分和复杂部分分开,再创建两个列表,但是仍然需要对它们进行数学运算。 Also, the final array may have values of m and n (greatly) exceeding 1000. (The final necessary size of the array won't be known until the end of the code.) 另外,最终数组的m和n值(大于)可能超过1000。(直到代码结束,才知道数组的最终必要大小。)

Any insight on being able to add null rows or columns to an initial array in a more efficient manner would be great. 能够以更有效的方式向初始数组添加空行或列的任何见识都将是很棒的。

Try this: 尝试这个:

np.concatenate((T, np.zeros((1,T.shape[1]), dtype=np.complex_)), axis=0)

and

np.concatenate((T, np.zeros((T.shape[0],1), dtype=np.complex_)), axis=1)

I'd suggest studying the code for np.insert . 我建议研究np.insert的代码。 It is Python. 是Python。 If it is complex it is simply because it is trying to be general, handling rows or cols etc. 如果它很复杂,那仅仅是因为它试图变得通用,处理行或列等。

The basic idea is to make a new result array of the correct size, and then copy blocks of values from the original to the result. 基本思想是制作一个具有正确大小的新结果数组,然后将值的块从原始值复制到结果中。 In 1d that would be something like: 在1d中,它类似于:

z = np.zeros(x.shape[0]+1, dtype=x.dtype)
z[:i] = x[:i]
z[i+1:] = x[i:]

That can be easily generalized to adding a row in a 2d ( z[:i,...] might be all that's needed). 可以很容易地将其概括为在2d中添加一行(可能仅需要z[:i,...] )。

To add a column as well as a row, I can imagine copying 4 blocks. 要添加列和行,我可以想象复制4个块。

It is also possible to use concatenate (which does a similar sort of block copy in compiled code). 也可以使用级联(在已编译的代码中执行类似的块复制)。

np.concatenate([x[:i],np.array([0]), x[i:]])

np.insert might be easier to use, but one way or other you'll end up doing this kind of block copy. np.insert可能更易于使用,但是您最终会以某种方式进行这种块复制。

Now if you want just add rows or columns (or both), you could use np.pad . 现在,如果您只想添加行或列(或同时添加两者),则可以使用np.pad That's very general and allows additions at the front and end in each dimension. 这很笼统,可以在每个维度的前端和后端添加内容。 np.insert is more useful if you want to add the new value(s) somewhere in the middle of the array. 如果要在数组中间的某个位置添加新值, np.insert会更有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM