chrisis
chrisis

Reputation: 119

ValueError: could not broadcast input array from shape (6) into shape (1)

I made a vector of constant values. The vector is (1 row, 6colums). Then, I call the vector in another function and I want to use each element of the vector for making other calcualtions. The problem is that I get the following error:

ValueError: could not broadcast input array from shape (6) into shape (1)

Why? Is there someone that can help me? Best regards,

n=6
F = np.ones([1,n])  
F = F*0.4

# F: [[0.4 0.4 0.4 0.4 0.4 0.4]] 
# Other function
str=np.zeros([1,n])
for i in range(0, len(F)):
    str[i] = 1000 * F[i]

# ValueError: could not broadcast input array from shape (6) into shape (1)

Upvotes: 1

Views: 2508

Answers (1)

Hoog
Hoog

Reputation: 2298

Your F is not a list of values, but a list of one list of values. I can see this by the extra [] surrounding your values. So when you do the calculation str[i] = 1000 * F[i] you are working with 2 lists. You could try replacing that line with str[0][i] = 1000 * F[0][i] to access the 0th element of the outerlist (which is just the list of values) and then picking the ith value from that list.

This might not be exactly the answer you are looking for, something that can help get good answers is to post the full traceback. Odds are you have much more to your error message than the one line ValueError, that extra information would be very useful posted in your questions! You could also add the definition for whatever Fd_ULS is.

Upvotes: 2

Related Questions