Arrays have a fixed size. You can't append to them. If you know the upper bounds of the array, you can preallocate the array with
np.empty((10, 10, 10))
For a 10x10x10 matrix. You can then keep 3 indices x,y,z to track the actual size you have. Eg, add a new element with:
matrix[x,y,z] = newElement
x += 1
Then when you're done, you can extract the submatrix with
finalMatrix = matrix[:x,:y,:z]
Answer from Oren Bell on Stack OverflowArrays have a fixed size. You can't append to them. If you know the upper bounds of the array, you can preallocate the array with
np.empty((10, 10, 10))
For a 10x10x10 matrix. You can then keep 3 indices x,y,z to track the actual size you have. Eg, add a new element with:
matrix[x,y,z] = newElement
x += 1
Then when you're done, you can extract the submatrix with
finalMatrix = matrix[:x,:y,:z]
You can use np.empty((x,y,z))
You can find a good explanation in this answer: How to create 3 dimensions matrix in numpy , like matlab a(:,:,:)
Initializing a 3D Numpy array with random values in Python - Python - Data Science Dojo Discussions
How can I create a truly empty numpy array which can be merged onto (by a recursive function)?
python - Creating an empty multidimensional array - Stack Overflow
Declaring a 3D array (list?) in Python
I'm kind of stuck conceptually on how to make this happen. I have a recursive method that builds a binary tree, and stores the tree as an instance variable. However, the function is not allowed to return anything, so each recursive call should (according to me) modify in-place the tree instance variable. However, I'm not sure how to set up my instance variable such that all said and done it holds a multidimensional array that represents the tree.
Say I set initialize it as a 1x1 array with element zero as a placeholder. Then as I go about recursing through my tree I can merge to it... but at the end I'm left with a spare [0] element that I don't need. In this case, I'd need some kind of final stop condition and function to remove that unnecessary placeholder stump. I don't think this is possible?
Otherwise, say I initialize the instance variable as None. Then when the first series of recursive calls, it would have to reassign the tree variable to change from None to an ndarray object, but all future calls would have to merge to the array. I don't think this is what the function should be asked to do?
Is there a way to make a truly empty array that I can merge onto? (e.g. np.empty doesn't reallly give an empty array, it gives an array with placeholder values so I'm still left with a useless stump at the end).
I'd suggest using np.full_like to choose the fill-value directly...
x = np.full_like((3, 1), None, dtype=object)
... of course the dtype you chose kind of defines what you mean by "empty"
I am guessing that by empty, you mean an array filled with zeros.
Use np.zeros() to create an array with zeros. np.empty() just allocates the array, so the numbers in there are garbage. It is provided as a way to even reduce the cost of setting the values to zero. But it is generally safer to use np.zeros().