2

A rather simple problem, and yet I dont seem to find any solution to it:

I am trying to use the differentiation of two arrays within an array and insert it as a new result into a new array... I hope the code clarifies this intention, even though it is not working, as python is telling me that it cannot combine a sequence with array calculation... How can I go around this problem? Thanks in advance!

MeteorNum = 5
#Coordinate Setup:

XPos = np.random.randint(0, 600, (MeteorNum,1))
YPos = np.random.randint(0, 800, (MeteorNum,1))

XYPos = np.hstack((XPos,YPos))
XYPos = np.vstack((XYPos, XYPos[0]))
print XYPos

#Distances:
r = np.zeros((MeteorNum,MeteorNum))

for i in range(0, MeteorNum):
    for x in range(0, MeteorNum):
        r[x,i] = (XYPos[x,:] - XYPos[i,:])
r = r*10**5
print r

and here the error I get:

runfile('C:/Users/Marco DS/Dropbox/1_TUDelft/4Q/AE1205 Python/python codes      2015/bonus assigments/week_5 Gravity simu.py', wdir='C:/Users/Marco   DS/Dropbox/1_TUDelft/4Q/AE1205 Python/python codes 2015/bonus assigments')
[[249 660]
 [256 605]
 [119 423]
 [422 398]
 [480 504]
 [249 660]]
Traceback (most recent call last):

  File "<ipython-input-57-4cd9cc29ece0>", line 1, in <module>
    runfile('C:/Users/Marco DS/Dropbox/1_TUDelft/4Q/AE1205 Python/python     codes 2015/bonus assigments/week_5 Gravity simu.py', wdir='C:/Users/Marco     DS/Dropbox/1_TUDelft/4Q/AE1205 Python/python codes 2015/bonus assigments')

  File "C:\Users\Marco DS\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
execfile(filename, namespace)

File "C:\Users\Marco DS\Anaconda\lib\site-    packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/Marco DS/Dropbox/1_TUDelft/4Q/AE1205 Python/python codes 2015/bonus assigments/week_5 Gravity simu.py", line 37, in <module>
r[x,i] = (XYPos[x,:] - XYPos[i,:])

ValueError: setting an array element with a sequence.
7
  • please show all the errors ... Commented May 20, 2015 at 13:39
  • 1
    Please show the entire error message (traceback), it should have a line number, the line it is stuck on, the function name, the module name, ... Commented May 20, 2015 at 13:43
  • What do you mean with differentiation? Just computing the difference of the left and the right column? Commented May 20, 2015 at 13:45
  • 1
    Is it intentional that you store the np.hstack and np.vstack in the same variable? Commented May 20, 2015 at 13:45
  • P.R. the vstack is in order to copy and add the first element to the last element of the array. That way, when I use the for loops, I can make sure all values are included. Commented May 20, 2015 at 14:01

2 Answers 2

2

try to add dtype= in the container array:

MeteorNum = 5
#Coordinate Setup:

XPos = nprand.randint(0, 600, (MeteorNum,1))
YPos = nprand.randint(0, 800, (MeteorNum,1))

XYPos = np.hstack((XPos,YPos))
XYPos = np.vstack((XYPos, XYPos[0]))
print XYPos

#Distances:
r = np.zeros((MeteorNum,MeteorNum),dtype=np.ndarray)

for i in range(0, MeteorNum):
    for x in range(0, MeteorNum):
        r[x,i] = (XYPos[x,:] - XYPos[i,:])
r = r*10**5
print r

EDIT

The dtype argument indicate the desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes! That worked for me! It now gives me a huge array, which is what I needed. However, all elements now also say "array" and then what is in it, but oh well. it does the job... What though does the dtype do?
I edited my answer to define the dtype argument role
@user3604362 you can accept the answer if it was helpful for you
1

To remove errors, I think you need an extra dimension, of size 2, in your array r:

r = np.zeros((MeteorNum,MeteorNum,2))

As XYPos[x,:] - XYPos[i,:] in your for loop has a shape of (2,)

This returns a single array of shape (5, 5, 2).

3 Comments

hmmm, no... this does work, however I end up with several arrays, whereas I only want one big array.
@user3604362 Really? r should be one array of shape (5, 5, 2). Use r.shape to check.
user, are you confusing a 3d array with several arrays? Do you understand the problem of trying to put 2 values into one slot of r?

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.