0

New to python and coding in general...

I have a 3D array with shape (62, 200, 400). I want to create a new 1D array with just values of axis = 0 which I belive is the 62 elements in my original 3D array.

How would I go about doing this? So far I've only been able to create a new 3D array by indexing with size (62, 0, 0).

Thanks!

This is how far I got with my code,

new_array = data[:,:0,:0]
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
  • 2
    `data[:,0,0]` does return a 1d array with length 62. But so does `data[:,156,345]` – hpaulj Feb 21 '23 at 01:32
  • 3
    Might be easier to think of this problem as a 3x3 rubik's cube. When you say you want a 1D array of axis=0, which part of the cube do you want? Are you looking for a single row on one face? All values on a particular face (i.e. that is a flattened 2D object)? – Mark Feb 21 '23 at 01:41

1 Answers1

0

It's easier to visualise this in 2D. Look at this array with shape (4, 3):

import numpy as np

a = np.array([[ 1,  2,  3],
              [ 4,  5,  6],
              [ 7,  8,  9],
              [10, 11, 12]])

Taking a slice a[:,0] will return a 1D array along axis 0:

>>> a[:,0]

array([ 1,  4,  7, 10])

As will slice a[:,1]:

>>> a[:,1]

array([ 2,  5,  8, 11])

And slice a[:,2]:

>>> a[:,2]

array([ 3,  6,  9, 12])

So you can't really say that along axis 0 there are only 4 values, because as you can see all of the 12 values stored in the array are available along axis 0. The shape of an array rather describes the length of it's axes, not how many values are stored along it.

So if you really looking for 4 values along axis 0, you have to decide which one of the slices you want. In the case this is not exactly what you want to achieve, please refine your question.

plasma_r
  • 116
  • 5