0

I have a list of length 50 created using linspace:

m=np.linspace(0,10,50)

I can recast this as a 10 X 5 matrix using reshape

X=np.reshape(m,(10,5))

But, if I want to use for-loops to do the same thing, I get an error:

z=np.zeros((10,5),dtype=float)
s=0
for i in range(0,10):
    for j in range(0,5):
        m[i][j]=z[s]
        s=s+1

here is the error:

'numpy.float64' object does not support item assignment

Why isn't the item assignment supported?

Thanks

1
  • What is the variable m? Your code assumes that it's already some 2D structure, but you've never defined it. Commented Apr 1, 2020 at 21:42

2 Answers 2

1

z will store your items from m after the reshape :

m=np.linspace(0,10,50)

z=np.zeros((10,5),dtype=float)
for i in range(10):
    for j in range(5):
        z[i][j]=m[i * 5 + j]  # compute the index from m base on the current row and column


print(z)
Sign up to request clarification or add additional context in comments.

Comments

0

It was a silly mistake:

yes, I got the sizes wrong; this is how I rewrote it:

m=np.linspace(0,10,50)
z=np.zeros((10,5),dtype=float)
s=0
for i in range(0,10):
    for j in range(0,5):
        z[i][j]=m[s]
        s=s+1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.