0

I have an array of unknown size - below 100 x 100 in it's dimensions; and I need to copy it into another Numpy array of a set size - 100 x 100.

emptyArray = np.empty(shape=[100,100])
fullArray = np.append(emptyArray, data1[y1:y2, x1:x2])

I need this so I can work with fullArray's of the same size later on in my code but this does not seem to be working. So the data would need to be appended into the top corner of the 100 x 100 array.

Does anyone have any advice on a how to do this? Be that an existing Numpy method or otherwise?

3
  • 1
    All NumPy arrays know their size my_array.shape. Python lists of lists also len(my_list). How can you have an array of unknown size? Commented Dec 6, 2015 at 20:11
  • Aside from Mike's correct comment, are you sure the array you want to copy into will always be of a bigger size, both in number of columns as well as number of rows? Commented Dec 6, 2015 at 20:15
  • I will know the size of the array yes, but it will always be below 100 rows and columns. Commented Dec 6, 2015 at 20:19

1 Answer 1

1

You should know the dimension of your smaller array but I presume from your question that it is smaller than 100x100 but varies from simulation to simulation. You can solve this like this:

nx,ny = smallerArray.shape
largeArray[0:nx,0:ny] = smallerArray[0:nx,0:ny]
Sign up to request clarification or add additional context in comments.

4 Comments

I'm slightly confused as to how this works? When I print largeArray[0:nx,0:ny], it gives the same as the smaller array but with each row as a new list? Rather than all the data from the smaller array in a larger array. So therefore if my smaller array were a different size would the larger array not be different also?
Are you talking about your solution or the one I provided?
Yours, I think I'm just a tad confused about how/why this would keep the largeArray as a constant size regardless of what size the smallerArray is. I can't tell whether or not it is right from printing as when I do I just see the elements from the smaller array.
Your solution is about appending new elements (as lists) to the existing largeArray, mine is just changing the existing elements.

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.