I'm trying to initialize a NumPy matrix of size (x,y) where y is very large.
The first column of the matrix is an ID (integer), and the rest are triplets (int8), where each member of the triplet should have a different default value.
i.e. assuming the default values are [2,5,9]
I'd like to initialize the following matrix:
0 2 5 9 2 5 9 2 5 9 ...
0 2 5 9 2 5 9 2 5 9 ...
0 2 5 9 2 5 9 2 5 9 ...
0 2 5 9 2 5 9 2 5 9 ...
...
The fastest way I could think of initializing the matrix is:
defaults = [2, 5, 9]
mat = numpy.zeros(shape=(x,y),
dtype=['i'] + ['int8'] * (y - 1))
# fill the triplets with default values
for i in range(1, y/3):
j = i * 3
mat[:, j] = defaults[0]
mat[:, j+1] = defaults[1]
mat[:, j+2] = defaults[2]
What is the fastest way to initialize such a matrix?
Thanks!