0

Say you have two numpy arrays one, call it A = [x1,x2,x3,x4,x5] which has all the x coordinates, then I have another array, call it B = [y1,y2,y3,y4,y5].. How would one "extract" a set of coordinates e.g (x1,y1) so that i could actually do something with it? Could I use a forloop or something similar? I can't seem to find any good examples, so if you could direct me or show me some I would be grateful.

5
  • to get a tuple p containing the first two elements of the arrays (x1,y1) you could use for instance p=(A[0],B[0]) Commented Jun 2, 2017 at 12:49
  • @user2314737 Good answer! Why did you post it as a comment? Commented Jun 2, 2017 at 12:50
  • @ashbygeek because it's friday :) Commented Jun 2, 2017 at 12:54
  • @user2314737 Haha, ok. Commented Jun 2, 2017 at 13:01
  • Does this answer your question? concatenate multiple numpy arrays in one array? Commented Nov 2, 2019 at 0:28

4 Answers 4

2

Not sure if that's what you're looking for. But you can use numpy.concatenate. You just have to add a fake dimension before with [:,None] :

import numpy as np
a = np.array([1,2,3,4,5])
b = np.array([6,7,8,9,10])

arr_2d = np.concatenate([a[:,None],b[:,None]], axis=1)
print arr_2d
# [[ 1  6] [ 2  7] [ 3  8] [ 4  9] [ 5 10]]

Once you have generated a 2D array you can just use arr_2d[i] to get the i-th set of coordinates.

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

Comments

1
import numpy as np

a = np.array([1, 2, 3, 4, 5])
b = np.array([6, 7, 8, 9, 10])

print(np.hstack([a[:, np.newaxis], b[:, np.newaxis]]))


[[ 1  6]
 [ 2  7]
 [ 3  8]
 [ 4  9]
 [ 5 10]]

Comments

0

As @user2314737 said in a comment, you could manually do it by simply grabbing the same element from each array like so:

a = np.array([1,2,3])
b = np.array([4,5,6])

index = 2 #completely arbitrary index choice

#as individual values
pointA = a[index]
pointB = b[index]

#or in tuple form
point = (a[index], b[index])

If you need all of them converted to coordinate form, then @Nuageux's answer is probably better

Comments

0

Let's say you have x = np.array([ 0.48, 0.51, -0.43, 2.46, -0.91]) and y = np.array([ 0.97, -1.07, 0.62, -0.92, -1.25])

Then you can use the zip function

zip(x,y)

This will create a generator. Turn this generator into a list and turn the result into a numpy array

np.array(list(zip(x,y)))

the result will look like this

array([[ 0.48,  0.97],
       [ 0.51, -1.07],
       [-0.43,  0.62],
       [ 2.46, -0.92],
       [-0.91, -1.25]])

Comments

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.