0

I have two np arrays, one is 1 dimensional, and the other it between 0 and 8 dimensional. I'm trying to append the multidimensional array onto the other array, as you would with a list. I've tried np.append(1dim, multidim) and np.concatenate([1dim, multidim]) but neither have worked.

[-33.752, 150.902, 38.022, 203.0, 1.0]

[[ -33.75   150.9     39.805    0.       1.   ]
 [ -33.75   150.902   44.697    1.       1.   ]
 [ -33.75   150.905   49.054    2.       1.   ]
 [ -33.752  150.905   39.062  204.       1.   ]
 [ -33.755  150.905   40.698  406.       1.   ]
 [ -33.755  150.902   37.512  405.       1.   ]
 [ -33.755  150.9     36.249  404.       1.   ]
 [ -33.752  150.9     36.627  202.       1.   ]]

to become:

[-33.752, 150.902, 38.022, 203.0, 1.0],
 [ -33.75   150.9     39.805    0.       1.   ]
 [ -33.75   150.902   44.697    1.       1.   ]
 [ -33.75   150.905   49.054    2.       1.   ]
 [ -33.752  150.905   39.062  204.       1.   ]
 [ -33.755  150.905   40.698  406.       1.   ]
 [ -33.755  150.902   37.512  405.       1.   ]
 [ -33.755  150.9     36.249  404.       1.   ]
 [ -33.752  150.9     36.627  202.       1.   ]]

I would like to be able to reference the multidim array by using 1dim[1]

4
  • Use a pure-Python tuple (_1d_array, mul_dim_array) for that... Commented Oct 15, 2016 at 6:26
  • Will the be any significant increase in runtime over large datasets by using a tuple vs np array? Commented Oct 15, 2016 at 6:27
  • 1
    With mixed sizes like there's no point making an array of arrays. Use concatenate if you want to join a (5,) array with a (8,5) one to produce a (9,5) array. But if the goal is a 2 element 'array', stick with a list. Commented Oct 15, 2016 at 6:40
  • Yeah if you say 8-dimensional I thought you mean an array of shape M×N×P×Q×R×S×T×U �?� Commented Oct 15, 2016 at 8:16

3 Answers 3

3

Your example output shows a (1x8) array concatenated with an (Nx8) array to form an (N+1 x 8) array.

If you want to create a "thing" where the first element is the 1-D array and the second element is the N-D array, a list or tuple is your bet. Numpy doesn't really have a great facility for this.

If you want to create an array that is the combination of the two 2D, based on your example, np.vstack([flat_array, big_array]) will do that.

By the way, when describing the number of dimensions something has, array.ndim is the same as len(array.shape). Note that an array with 5 rows and 8 columns is two dimensional in the context of numpy.

0

You have to use numpy.append or numpy.concatenate. But careful about the shape of your array. For your case you have to define the correct shape

ans=np.append([[small array]],[big array], axis=0) (Note that the extra [] is for correct shape, read the numpy.append tutorial)

0
0

Or you can do it manually for any shape

blank= [ ] #create a blank list
a=1D array
blank.append(a)
b=nD array
for i in range(len(b)):
     blank.append(b[i])
answer=np.array(blank)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.