2

i have 3 numpy arrays which store image data of shape (4,100,100).

arr1= np.load(r'C:\Users\x\Desktop\py\output\a1.npy')
arr2= np.load(r'C:\Users\x\Desktop\py\output\a2.npy')
arr3= np.load(r'C:\Users\x\Desktop\py\output\a3.npy')

I want to merge all 3 arrays into 1 array. I have tried in this way:

merg_arr = np.zeros((len(arr1)+len(arr2)+len(arr3), 4,100,100), dtype=input_img.dtype)

now this make an array of the required length but I don't know how to copy all the data in this array. may be using a loop?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
FJ_Abbasi
  • 413
  • 2
  • 5
  • 16
  • 1
    What you man by merge into one array? do you want to concatenate them? do you want to sum up the pixel values? What would be the final shape of your array? – Mazdak May 08 '17 at 12:33
  • Oh.. i missed the `(len(arr1)+len(arr2)+len(arr3), 4, ...` part. Can you explaian why you want a shape of [12, 4, 100, 100]? Either [3, 4, 100, 100] or [12, 100, 100] would seem to make more sense. – MB-F May 08 '17 at 12:38
  • i have 3 arrays of shape (60, 4,100,100), (14, 4,100,100), (6, 4,100,100).. I want them in a single array of shape (80,4,100,100) – FJ_Abbasi May 08 '17 at 12:42
  • I'm taking off the duplicate flag. That SO question dealt with 1d arrays, which need `vstack`. Here the arrays have the right shape to concatenate on the 1st dimension. It's evident from the comments that the problem isn't with concatenate. Something else is going on. – hpaulj May 08 '17 at 16:40

1 Answers1

7

This will do the trick:

merge_arr = np.concatenate([arr1, arr2, arr3], axis=0)

np.stack arranges arrays along a new dimension. Their dimensions (except for the first) need to match.

Demo:

arr1 = np.empty((60, 4, 10, 10))
arr2 = np.empty((14, 4, 10, 10))
arr3 = np.empty((6, 4, 10, 10))
merge_arr = np.concatenate([arr1, arr2, arr3], axis=0)
print(merge_arr.shape)  # (80, 4, 10, 10)
MB-F
  • 22,770
  • 4
  • 61
  • 116
  • all of them are of different size, like (60,4,100,100), (14,4,100,100),(6,4,100,100) – FJ_Abbasi May 08 '17 at 12:36
  • @FahadJahangir you did not say so in the question ;) – MB-F May 08 '17 at 12:39
  • so here is the problem! the resultant array has all zeros in it. like this: [[[[0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0] ..., – FJ_Abbasi May 08 '17 at 12:57
  • @FahadJahangir time to check if your input arrays have all zeros. Or maybe there are zeros on the borders only. Try `np.unique(merge_arr)` to see if there are really only zeros in an array. – MB-F May 08 '17 at 13:05
  • well the input arrays don't have zeros.. they have data like [[[[215 185 118 ..., 60 25 18] [228 185 90 ..., 42 18 25] [219 172 100 ..., 18 18 81] ..., [240 172 100 ..., 118 118 134] [209 100 70 ..., 81 81 118] [126 25 60 ..., 60 51 90]] – FJ_Abbasi May 08 '17 at 13:07
  • @FahadJahangir there must be a mistake somewhere in your code.. can't clarify that in comments, though. Best create a [minimal, complete, and verifyable example](http://stackoverflow.com/help/mcve) with toy data and if you can reproduce the problem ask an new specific question. – MB-F May 08 '17 at 13:11