简体   繁体   中英

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. 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. 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). Essentially, all I want to do is add either a zero row or column (or both) to the initial array.

case 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:

[ 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:

[ 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:

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?

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.)

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 . It is 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:

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).

To add a column as well as a row, I can imagine copying 4 blocks.

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.

Now if you want just add rows or columns (or both), you could use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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