Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Matrix initialization with ascending numbers for rows

I try to have a matrix like:

M= [[1,1,..,1],
    [2,2,..,2],
    ...
    [40000, 40000, ..,40000]

It's what I tried:

data = np.mat((40000,8))
print(data.shape)
for i in range(data.shape[0]):
     data[i,:] = i

print(data[:5])

The above code prints:

(1, 2)
[[0 0]]

I know how to fill a matrix with constant values, but I couldn't find a similar question for this case.

like image 797
Ahmad Avatar asked Jan 21 '26 10:01

Ahmad


1 Answers

Use a simple array and don't forget that Python starts indexing at 0:

data = np.zeros((40000,8))
for i in range(data.shape[0]):
     data[i,:] = i+1
like image 83
Matthieu Brucher Avatar answered Jan 23 '26 00:01

Matthieu Brucher