0

I have,

import numpy as np

distances.append(np.array(ranges))
theta = np.arange(angle_min, angle_max + angle_increment, angle_increment)
theta = np.degrees(theta)

something.append(np.cos(np.radians(theta)) * distances[-1])

but it doesn't run for all files.

ValueError: operands could not be broadcast together with shapes (1082,) (1081,)

The error is something.append(np.cos(np.radians(theta)) * distances[-1]) but why??

  • What this error means is that the size of your matrices does not allow the operation you are trying to perform. In your case, the `np.cos(np.radians(theta))` has an element more (1082) than the `distances[-1]`. Note that if e.g., you start from an angle of 0 and go all the way to 90° **inclusive**, with a step of 1°, you are going to have **91** values! Take a look at this for more: http://stackoverflow.com/questions/24560298/python-numpy-valueerror-operands-could-not-be-broadcast-together-with-shapes – Ma0 Sep 06 '16 at 09:34
  • @Ev.Kounis If I write 'something.append(np.cos(np.radians(theta)) * distances)' . I take back **ValueError: operands could not be broadcast together with shapes (1082,) (1,1081) ** –  Sep 06 '16 at 09:45
  • the `distances[-1]` was not the problem. the problem is that the `distances[-1]` array has less elements (smaller dimension vector) than the `np.cos(np.radians(theta))`. The `distances[-1]` does not mean `-1 element`. It means from the distances container, the last object (which in this case is an array). Google `python slicing` for more on that. – Ma0 Sep 06 '16 at 10:02

0 Answers0