Fastest method to create 2D numpy array whose elements are in range

Froyo

I want to create a 2D numpy array where I want to store the coordinates of the pixels such that numpy array looks like this

[(0, 0), (0, 1), (0, 2), ...., (0, 510), (0, 511)
 (1, 0), (1, 1), (1, 2), ...., (1, 510), (1, 511)
 ..
 ..
 ..
 (511, 0), (511, 1), (511, 2), ...., (511, 510), (511, 511)]

This is a ridiculous question but I couldn't find anything yet.

Daniel

Can use np.indices or np.meshgrid for more advanced indexing:

>>> data=np.indices((512,512)).swapaxes(0,2).swapaxes(0,1)
>>> data.shape
(512, 512, 2)

>>> data[5,0]
array([5, 0])
>>> data[5,25]
array([ 5, 25])

This may look odd because its really made to do something like this:

>>> a=np.ones((3,3))
>>> ind=np.indices((2,1))
>>> a[ind[0],ind[1]]=0
>>> a
array([[ 0.,  1.,  1.],
       [ 0.,  1.,  1.],
       [ 1.,  1.,  1.]])

A mgrid example:

np.mgrid[0:512,0:512].swapaxes(0,2).swapaxes(0,1)

A meshgrid example:

>>> a=np.arange(0,512)
>>> x,y=np.meshgrid(a,a)
>>> ind=np.dstack((y,x))
>>> ind.shape
(512, 512, 2)

>>> ind[5,0]
array([5, 0])

All are equivalent ways of doing this; however, meshgrid can be used to create non-uniform grids.

If you do not mind switching row/column indices you can drop the final swapaxes(0,1).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select elements within certain range per column in a 2D NumPy array

From Dev

How do I quickly create a numpy or pandas 2D array with both axis in a range and the values a product?

From Dev

Fastest way of iterating and accessing elements of numpy array?

From Dev

numpy - fastest way to build 2d array with permuted copies of numpy 1d array

From Dev

how to create method with 2d array

From Dev

edit a range of elements inside a 2d array?

From Dev

What is the fastest way to set an arbitrary range of elements in a Java array to null?

From Dev

python/numpy fastest method for 2d kernel rank filtering on masked arrays (and/or selective ranking)

From Dev

Apply function to 2D numpy array elements

From Dev

NumPy speed up setting elements of 2D Array

From Dev

remove elements from 2D numpy array based on proximity

From Dev

minus specify elements in 2D array numpy

From Dev

Numpy 2d array - select multiple elements without a for loop

From Dev

Fastest way to append nonzero numpy array elements to list

From Dev

numpy fastest way to transform an array's elements to their frequency

From Dev

Fastest way to append nonzero numpy array elements to list

From Dev

Create an array from range elements with a base array

From Dev

Fastest way to list diagonal elements of a 2D array from a given point

From Dev

Numpy: Fastest way of computing diagonal for each row of a 2d array

From Dev

Fastest way to convert a list of indices to 2D numpy array of ones

From Dev

Method to multiply elements of 2d array by element of another 2d array JAVA

From Dev

How to create a 2d numpy array from a list of tuples

From Dev

Replace 2D numpy array elements based on 2D indexes

From Dev

Fastest way to create a numpy array from text file

From Dev

Create numpy array with proper step and step range

From Dev

Numpy: get the lowest N elements of an array X, considering only elements whose index is not an element in another array Y

From Dev

Is there any fastest approach to reverse the range of elements in an array consisting of two types of elements?

From Dev

Numpy: get the index of the elements of a 1d array as a 2d array

From Dev

Numpy fastest 3D to 2D projection

Related Related

  1. 1

    Select elements within certain range per column in a 2D NumPy array

  2. 2

    How do I quickly create a numpy or pandas 2D array with both axis in a range and the values a product?

  3. 3

    Fastest way of iterating and accessing elements of numpy array?

  4. 4

    numpy - fastest way to build 2d array with permuted copies of numpy 1d array

  5. 5

    how to create method with 2d array

  6. 6

    edit a range of elements inside a 2d array?

  7. 7

    What is the fastest way to set an arbitrary range of elements in a Java array to null?

  8. 8

    python/numpy fastest method for 2d kernel rank filtering on masked arrays (and/or selective ranking)

  9. 9

    Apply function to 2D numpy array elements

  10. 10

    NumPy speed up setting elements of 2D Array

  11. 11

    remove elements from 2D numpy array based on proximity

  12. 12

    minus specify elements in 2D array numpy

  13. 13

    Numpy 2d array - select multiple elements without a for loop

  14. 14

    Fastest way to append nonzero numpy array elements to list

  15. 15

    numpy fastest way to transform an array's elements to their frequency

  16. 16

    Fastest way to append nonzero numpy array elements to list

  17. 17

    Create an array from range elements with a base array

  18. 18

    Fastest way to list diagonal elements of a 2D array from a given point

  19. 19

    Numpy: Fastest way of computing diagonal for each row of a 2d array

  20. 20

    Fastest way to convert a list of indices to 2D numpy array of ones

  21. 21

    Method to multiply elements of 2d array by element of another 2d array JAVA

  22. 22

    How to create a 2d numpy array from a list of tuples

  23. 23

    Replace 2D numpy array elements based on 2D indexes

  24. 24

    Fastest way to create a numpy array from text file

  25. 25

    Create numpy array with proper step and step range

  26. 26

    Numpy: get the lowest N elements of an array X, considering only elements whose index is not an element in another array Y

  27. 27

    Is there any fastest approach to reverse the range of elements in an array consisting of two types of elements?

  28. 28

    Numpy: get the index of the elements of a 1d array as a 2d array

  29. 29

    Numpy fastest 3D to 2D projection

HotTag

Archive