In Python, I want to create an n-by-100
matrix, where the value of n
is unknown at the start. This creation involves successively adding a row to the matrix. The code I have tried for this is:
x = numpy.zeros(100)
while true:
y = loadrow(); # Load a 1-by-100 NumPy array for the new row
x = numpy.append(x, y, 0)
However, there are three issues with the above, which I am having difficulty solving:
The line
x = numpy.zeros(100)
initialises the matrix with a row of 100 zeros. However, I want the first row to be the first one I load. How can I create an empty matrix which will only be given data once I append the first row?The line
x = numpy.append(x, y, 0)
doesn't add another row to the matrixx
. Instead, it just addsy
to the end of the first row, to create an even longer row. But if I tryx = numpy.append(x, y, 1)
, such that I append to axis 1, then I get the error:TypeError: Required argument 'object' (pos 1) not found
.When I successively append rows like this, it seems the I keep making copies of the original array, which will be inefficient as the array grows. Is there any other way to do this when I don't know what the size of the final array will be?
Thank you!