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

Time:12-08

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)

CodePudding user response:

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.

CodePudding user response:

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

  • Related