Reputation: 173
I'm kinda new to python, currently working on a project and getting this error with this lines of code.
g1_coll[obstacle==0]=tau*(g1+g2-g3+g4)
g2_coll[obstacle==0]=tau*(g1+g2+g3-g4)
g3_coll[obstacle==0]=tau*(-g1+g2+g3+g4)
g4_coll[obstacle==0]=tau*(g1-g2+g3+g4)
can anyone help me understand this?
Upvotes: 5
Views: 12593
Reputation: 2816
I think the other answers are mentioning to the source of the problem (e.g., melqkiades answer). I try to reproduce the problem in another way inspiring from this post.
So, such a mistake will be happened if we use np.matrix
(which is 2D - it is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future) instead of np.array
, that I tried to explain in the following code:
# Note that using np.array instead of np.matrix will get a 1D array with shape --> (4,) ==1D==> 1D res is expected
g_coll = np.matrix([0.94140464, 0.96913727, 0.43559733, 0.45494222]) # shape --> (1, 4) ==2D==> 2D res may be expected wrongly
# [[0.94140464 0.96913727 0.43559733 0.45494222]]
my_boolean_array = g_coll < 0.5 # shape --> (1, 4)
# [[False False True True]]
# g_coll[my_boolean_array] # shape --> (1, 2)
# [[0.43559733 0.45494222]]
# The point is here, where we are expecting res to be 2D, wrongly, because g_coll[my_boolean_array] is in 2D, but that must be 1D
res = np.array([[0, 0]]) # shape --> (1, 2)
g_coll[my_boolean_array] = res # --> res must be 1D: np.array([0, 0])
# The true answer will be as:
# res = np.array([0, 0]) # 1D --> shape: (2,)
# g_coll[my_boolean_array] = res
# # [[0.94140464 0.96913727 0. 0. ]]
Upvotes: 1
Reputation: 2649
The following should probably work as well
mask = (obstacle == 0)
new_array = tau*(g1+g2-g3+g4)
g1_coll[mask]= new_array[mask]
Notice the last [mask]
Upvotes: 1
Reputation: 473
I Assume the error you are getting is because all of your arrays are 2-dimensional. I suggest you try using numpy.putmask(matrix, mask, new_matrix_values)
For instance
mask = (obstacle == 0)
numpy.putmask(g1_coll, mask, tau*(g1+g2-g3+g4))
numpy.putmask(g2_coll, mask, tau*(g1+g2+g3-g4))
numpy.putmask(g3_coll, mask, tau*(-g1+g2+g3+g4))
numpy.putmask(g4_coll, mask, tau*(g1-g2+g3+g4))
Upvotes: 9
Reputation: 4629
The problem is what you are assigning using the mask. Not knowing what's inside g1, g2, g3 and g4 it's quite difficult to understand what you are doing, but probably
tau*(g1+g2-g3+g4)
is a vector of two dimension. Instead you need to assign a single value. For example, if you change your assignment in this way, it will probably work:
g1_coll[obstacle==0]=(tau*(g1+g2-g3+g4))[0]
g2_coll[obstacle==0]=(tau*(g1+g2+g3-g4))[0]
g3_coll[obstacle==0]=(tau*(-g1+g2+g3+g4))[0]
g4_coll[obstacle==0]=(tau*(g1-g2+g3+g4))[0]
or, if it is not working:
g1_coll[obstacle==0]=(tau*(g1+g2-g3+g4))[0][0]
g2_coll[obstacle==0]=(tau*(g1+g2+g3-g4))[0][0]
g3_coll[obstacle==0]=(tau*(-g1+g2+g3+g4))[0][0]
g4_coll[obstacle==0]=(tau*(g1-g2+g3+g4))[0][0]
But before doing anything you should understand what's inside your input (tau*(g1+g2-g3+g4)
).
My guess is that probably g1, g2, g3, and g4 are vectors of two dimensions
With this example I can reproduce your error:
import numpy as np
import random
my_matrix = np.random.rand(4)
print(my_matrix)
my_boolean_array = my_matrix < 0.5
print(my_boolean_array)
my_matrix[my_boolean_array] = [[0, 0]] # two dimensions array! not a single value. This will not work
print(my_matrix)
Try to print the value inside
print(tau*(g1+g2-g3+g4))
Upvotes: 0