ExcitedSnail
ExcitedSnail

Reputation: 193

How to correct this python code (fill up a matrix row by row)?

I want to fill up the matrix called allsamples using elements in A. I wrote the following code, however, I got the error message: " allsamples[i,:]=[a1, a2, a3, a4] IndexError: index 0 is out of bounds for axis 0 with size 0". How to correct the following code to make it work? Thanks!

 import numpy as np
A=[2, 4, 9, 10]
lA=len(A)
i=0
allsamples=np.zeros((lA^(lA),lA))
for a1 in A:
    for a2 in A:
        for a3 in A:
            for a4 in A:
                allsamples[i,:]=[a1, a2, a3, a4]
                i=i+1
print(allsamples)

Upvotes: 0

Views: 41

Answers (2)

LittlePanic404
LittlePanic404

Reputation: 145

allsamples have a shape of (0,4). So it might be the problem. lA^(lA) gives zero.

Upvotes: 1

defladamouse
defladamouse

Reputation: 625

Assuming the rest of the code is correct, the error is here (lA^(lA),lA). ^ is bitwise xor (https://wiki.python.org/moin/BitwiseOperators). Modify it to (lA**lA, lA) and it runs without error.

Upvotes: 1

Related Questions