0

I have two arrays of size 4,1,2

 array1  [[[   1.    2.]]
          [[   3.    4.]]
          [[   5.    6.]]
          [[   7.    8.]]]

 array2  [[[   8.    4.]]
          [[   7.    3.]]
          [[   6.    2.]]
          [[   5.    1.]]]

I need to put them into an array that looks like

 merged  [[[   1.    2.]]
          [[   3.    4.]]
          [[   5.    6.]]
          [[   7.    8.]]
          [[   8.    4.]]
          [[   7.    3.]]
          [[   6.    2.]]
          [[   5.    1.]]]

I then need to find the min and max of column1 and column2. I had this, but it does not appear to be doing what I think it should be.

mergedArray = concatenate((array1,array2),axis=1)

x_min = np.amin(mergedArray[:,0])
x_max = np.amax(mergedArray[:,0])
y_min = np.amin(mergedArray[:,1])
y_max = np.amax(mergedArray[:,1])

Is there a better way to do this?

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
Tim R
  • 514
  • 1
  • 8
  • 24

1 Answers1

0

I'd be tempted to remove the 'extra' dimension first, to keep the subsequent code simple.

Initialise the arrays:

>>> array1 = np.array([[[1, 2]], [[3, 4]], [[5, 6]], [[7, 8]]])
>>> array2 = np.array([[[8, 4]], [[7, 3]], [[6, 2]], [[5, 1]]])

>>> array1
array([[[1, 2]],

       [[3, 4]],

       [[5, 6]],

       [[7, 8]]])

Simplify the arrays:

>>> array1b = array1[:,0,:]
>>> array2b = array2[:,0,:]

>>> array1b
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])

Concatenation and min/max are now a little simpler:

>>> merged = np.concatenate((array1b, array2b))
>>> merged
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8],
       [8, 4],
       [7, 3],
       [6, 2],
       [5, 1]])

>>> np.amin(merged, axis=1)
array([1, 3, 5, 7, 4, 3, 2, 1])

Does this give you what you need?

jlb83
  • 1,988
  • 1
  • 19
  • 30
  • I ended up using np.vstack to get them in the right columns, and then looked at column 0 for min and max, and then column 1. – Tim R Mar 17 '15 at 18:21