6

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?

4
  • 5
    You should provide the structure of the variable g1_coll and obstacle to make the question more understandable
    – Nikaido
    Feb 5, 2020 at 15:33
  • lang indentifer added
    – B25Dec
    Feb 5, 2020 at 20:18
  • @Nikaido the problem relies on "obstacle", it is a matrix (400,600), the thing is that i nedd to make g1_coll 0 obstacle is 0, i thought of making a while cicle or an if statement but not sure if it works the same way as in matlab that i'm more familiar to
    – jsp
    Feb 6, 2020 at 20:55
  • 3
    please, add the structure of g1, g2, etc. Otherwise any answer attempt will be just speculation. For now, it's -1 because is not informative for the community May 29, 2020 at 8:28

4 Answers 4

9

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))
1

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]

1

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.        ]]
0

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))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.