0

I have two set of given numbers (100,110), and (20, 30). I wanted get numbers between them.

X = np.arange(100, 110)
Y = np.arange(20, 30)

print (X)
print (Y)

[100 101 102 103 104 105 106 107 108 109]
[20 21 22 23 24 25 26 27 28 29]

I wanted to get their positions as follows.

xy = np.array( [(x,y) for x in X  for y in Y])
print (xy)


X_result = xy[:,0]
Y_result = xy[:,1]

The results are correct.

However, wondering if it could be obtained more directly and more faster.

Expected results are same as shown by the prints of (X_result and Y_result).

print (X_result)
print (Y_result)

[100 100 100 100 100 100 100 100 100 100 101 101 101 101 101 101 101 101
 101 101 102 102 102 102 102 102 102 102 102 102 103 103 103 103 103 103
 103 103 103 103 104 104 104 104 104 104 104 104 104 104 105 105 105 105
 105 105 105 105 105 105 106 106 106 106 106 106 106 106 106 106 107 107
 107 107 107 107 107 107 107 107 108 108 108 108 108 108 108 108 108 108
 109 109 109 109 109 109 109 109 109 109]
[20 21 22 23 24 25 26 27 28 29 20 21 22 23 24 25 26 27 28 29 20 21 22 23
 24 25 26 27 28 29 20 21 22 23 24 25 26 27 28 29 20 21 22 23 24 25 26 27
 28 29 20 21 22 23 24 25 26 27 28 29 20 21 22 23 24 25 26 27 28 29 20 21
 22 23 24 25 26 27 28 29 20 21 22 23 24 25 26 27 28 29 20 21 22 23 24 25
 26 27 28 29]

Edit.

I noticed that what I wanted is:

X_result, Y_result = np.meshgrid(X, Y)

print (X_result.flatten())
print (Y_result.flatten())

Please let me know if there is other better ways of doing it.

1 Answer 1

1

You can use numpy.meshgrid:

np.meshgrid(X, Y, indexing='ij')

[array([[100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
       [101, 101, 101, 101, 101, 101, 101, 101, 101, 101],
       [102, 102, 102, 102, 102, 102, 102, 102, 102, 102],
       [103, 103, 103, 103, 103, 103, 103, 103, 103, 103],
       [104, 104, 104, 104, 104, 104, 104, 104, 104, 104],
       [105, 105, 105, 105, 105, 105, 105, 105, 105, 105],
       [106, 106, 106, 106, 106, 106, 106, 106, 106, 106],
       [107, 107, 107, 107, 107, 107, 107, 107, 107, 107],
       [108, 108, 108, 108, 108, 108, 108, 108, 108, 108],
       [109, 109, 109, 109, 109, 109, 109, 109, 109, 109]]), array([[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])]
Sign up to request clarification or add additional context in comments.

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.