In [1]:
%matplotlib inline
import numpy as np
import os

import matplotlib.pyplot as plt
plt.style.use('bmh')

In [2]:
x_min = 0
y_min = 0
x_max = 2 # x_max
y_max = 1 # y_max

# rectangular grid of points
# specify resolution
Nx = 10
Ny = 5
# specify limit of particle rectangle
particles_x_min = 0.9
particles_x_max = 1.1
particles_y_min = 0.4
particles_y_max = 0.6

# particles_x_min = 0
# particles_x_max = 2
# particles_y_min = 0
# particles_y_max = 1

paritcle_n = Nx*Ny

In [3]:
# create lists of all x and y positions
X  = np.linspace(particles_x_min, particles_x_max, Nx, endpoint = False)
Y  = np.linspace(particles_y_min, particles_y_max, Ny, endpoint = False)

In [4]:
X


Out[4]:
array([ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,  1.08])

In [5]:
Y


Out[5]:
array([ 0.4 ,  0.44,  0.48,  0.52,  0.56])

In [6]:
#XY = np.zeros((2, Ny, Nx))
# Use meshgrid to turn lists into rank 2 arrays
# of x and y positions
XY = np.meshgrid(X, Y)
XY


Out[6]:
[array([[ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08],
        [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08],
        [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08],
        [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08],
        [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08]]),
 array([[ 0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ],
        [ 0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,
          0.44],
        [ 0.48,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,
          0.48],
        [ 0.52,  0.52,  0.52,  0.52,  0.52,  0.52,  0.52,  0.52,  0.52,
          0.52],
        [ 0.56,  0.56,  0.56,  0.56,  0.56,  0.56,  0.56,  0.56,  0.56,
          0.56]])]

In [9]:
np.shape(XY)


Out[9]:
(2, 5, 10)

In [36]:
particle_id = np.arange(paritcle_n)
particle_id


Out[36]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])

In [37]:
particle_x = np.hstack(XY[:][0])
particle_x


Out[37]:
array([ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
        1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,
        1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,
        1.04,  1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,
        1.02,  1.04,  1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,
        1.  ,  1.02,  1.04,  1.06,  1.08])

In [38]:
particle_y = np.hstack(XY[:][1])
particle_y


Out[38]:
array([ 0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,
        0.4 ,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,
        0.44,  0.44,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,
        0.48,  0.48,  0.48,  0.52,  0.52,  0.52,  0.52,  0.52,  0.52,
        0.52,  0.52,  0.52,  0.52,  0.56,  0.56,  0.56,  0.56,  0.56,
        0.56,  0.56,  0.56,  0.56,  0.56])

In [10]:
# coordinates directly
x  = np.linspace(particles_x_min, particles_x_max, paritcle_n , endpoint = False)
y  = np.linspace(particles_y_min, particles_y_max, paritcle_n, endpoint = False)
x


Out[10]:
array([ 0.9  ,  0.904,  0.908,  0.912,  0.916,  0.92 ,  0.924,  0.928,
        0.932,  0.936,  0.94 ,  0.944,  0.948,  0.952,  0.956,  0.96 ,
        0.964,  0.968,  0.972,  0.976,  0.98 ,  0.984,  0.988,  0.992,
        0.996,  1.   ,  1.004,  1.008,  1.012,  1.016,  1.02 ,  1.024,
        1.028,  1.032,  1.036,  1.04 ,  1.044,  1.048,  1.052,  1.056,
        1.06 ,  1.064,  1.068,  1.072,  1.076,  1.08 ,  1.084,  1.088,
        1.092,  1.096])

In [11]:
y


Out[11]:
array([ 0.4  ,  0.404,  0.408,  0.412,  0.416,  0.42 ,  0.424,  0.428,
        0.432,  0.436,  0.44 ,  0.444,  0.448,  0.452,  0.456,  0.46 ,
        0.464,  0.468,  0.472,  0.476,  0.48 ,  0.484,  0.488,  0.492,
        0.496,  0.5  ,  0.504,  0.508,  0.512,  0.516,  0.52 ,  0.524,
        0.528,  0.532,  0.536,  0.54 ,  0.544,  0.548,  0.552,  0.556,
        0.56 ,  0.564,  0.568,  0.572,  0.576,  0.58 ,  0.584,  0.588,
        0.592,  0.596])

In [13]:
# 2-rank array:
XY = [x, y]
XY


Out[13]:
[array([ 0.9  ,  0.904,  0.908,  0.912,  0.916,  0.92 ,  0.924,  0.928,
         0.932,  0.936,  0.94 ,  0.944,  0.948,  0.952,  0.956,  0.96 ,
         0.964,  0.968,  0.972,  0.976,  0.98 ,  0.984,  0.988,  0.992,
         0.996,  1.   ,  1.004,  1.008,  1.012,  1.016,  1.02 ,  1.024,
         1.028,  1.032,  1.036,  1.04 ,  1.044,  1.048,  1.052,  1.056,
         1.06 ,  1.064,  1.068,  1.072,  1.076,  1.08 ,  1.084,  1.088,
         1.092,  1.096]),
 array([ 0.4  ,  0.404,  0.408,  0.412,  0.416,  0.42 ,  0.424,  0.428,
         0.432,  0.436,  0.44 ,  0.444,  0.448,  0.452,  0.456,  0.46 ,
         0.464,  0.468,  0.472,  0.476,  0.48 ,  0.484,  0.488,  0.492,
         0.496,  0.5  ,  0.504,  0.508,  0.512,  0.516,  0.52 ,  0.524,
         0.528,  0.532,  0.536,  0.54 ,  0.544,  0.548,  0.552,  0.556,
         0.56 ,  0.564,  0.568,  0.572,  0.576,  0.58 ,  0.584,  0.588,
         0.592,  0.596])]

In [14]:
np.array([x, y])


Out[14]:
array([[ 0.9  ,  0.904,  0.908,  0.912,  0.916,  0.92 ,  0.924,  0.928,
         0.932,  0.936,  0.94 ,  0.944,  0.948,  0.952,  0.956,  0.96 ,
         0.964,  0.968,  0.972,  0.976,  0.98 ,  0.984,  0.988,  0.992,
         0.996,  1.   ,  1.004,  1.008,  1.012,  1.016,  1.02 ,  1.024,
         1.028,  1.032,  1.036,  1.04 ,  1.044,  1.048,  1.052,  1.056,
         1.06 ,  1.064,  1.068,  1.072,  1.076,  1.08 ,  1.084,  1.088,
         1.092,  1.096],
       [ 0.4  ,  0.404,  0.408,  0.412,  0.416,  0.42 ,  0.424,  0.428,
         0.432,  0.436,  0.44 ,  0.444,  0.448,  0.452,  0.456,  0.46 ,
         0.464,  0.468,  0.472,  0.476,  0.48 ,  0.484,  0.488,  0.492,
         0.496,  0.5  ,  0.504,  0.508,  0.512,  0.516,  0.52 ,  0.524,
         0.528,  0.532,  0.536,  0.54 ,  0.544,  0.548,  0.552,  0.556,
         0.56 ,  0.564,  0.568,  0.572,  0.576,  0.58 ,  0.584,  0.588,
         0.592,  0.596]])

In [65]:
plt.scatter(particle_x, particle_y)
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)


Out[65]:
(0, 1)

In [66]:
XY[0][:]


Out[66]:
array([[ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8],
       [ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8],
       [ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8],
       [ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8],
       [ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8]])

In [67]:
XY[:][0]


Out[67]:
array([[ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8],
       [ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8],
       [ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8],
       [ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8],
       [ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8]])

In [79]:
a = np.array([[1, 2], [3, 4]])
a


Out[79]:
array([[1, 2],
       [3, 4]])

In [78]:
a[0][:]


Out[78]:
array([1, 2])

In [80]:
a[:][0]


Out[80]:
array([1, 2])

In [86]:
a[:][1]


Out[86]:
array([3, 4])

In [94]:
a[0]


Out[94]:
array([1, 2])

In [100]:
particle_id[0::4]


Out[100]:
array([ 0,  4,  8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48])

In [101]:
particle_id[1::4]


Out[101]:
array([ 1,  5,  9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49])

In [102]:
particle_id[2::4]


Out[102]:
array([ 2,  6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46])

In [103]:
particle_id[3::4]


Out[103]:
array([ 3,  7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47])

In [104]:
particle_id[4::4]


Out[104]:
array([ 4,  8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48])

In [10]:
X


Out[10]:
array([ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,  1.08])

In [12]:
b=[1,0,0,1]

In [13]:
b


Out[13]:
[1, 0, 0, 1]

In [14]:
X[b]


Out[14]:
array([ 0.92,  0.9 ,  0.9 ,  0.92])

In [17]:
p = np.arange(10)
p


Out[17]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [29]:
b = np.ones(XY[0][:].size, dtype=bool)

In [31]:
b[10:] = 0
b


Out[31]:
array([ True,  True,  True, False, False,  True,  True,  True,  True,
        True, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False], dtype=bool)

In [21]:
p[b]


Out[21]:
array([0, 1, 2, 5, 6, 7, 8, 9])

In [22]:
XY


Out[22]:
[array([[ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08],
        [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08],
        [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08],
        [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08],
        [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
          1.08]]),
 array([[ 0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ],
        [ 0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,
          0.44],
        [ 0.48,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,
          0.48],
        [ 0.52,  0.52,  0.52,  0.52,  0.52,  0.52,  0.52,  0.52,  0.52,
          0.52],
        [ 0.56,  0.56,  0.56,  0.56,  0.56,  0.56,  0.56,  0.56,  0.56,
          0.56]])]

In [26]:
XY[0][:]


Out[26]:
array([[ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
         1.08],
       [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
         1.08],
       [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
         1.08],
       [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
         1.08],
       [ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
         1.08]])

In [32]:
XY[0][b]


C:\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 5 but corresponding boolean dimension is 50
  if __name__ == '__main__':
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-32-87ae3cfc8fb0> in <module>()
----> 1 XY[0][b]

IndexError: index 5 is out of bounds for axis 0 with size 5

In [35]:
XY[0,:]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-f26144db4931> in <module>()
----> 1 XY[0,:]

TypeError: list indices must be integers or slices, not tuple

In [34]:
XY.shape


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-34-0c022e046fe1> in <module>()
----> 1 XY.shape

AttributeError: 'list' object has no attribute 'shape'

In [43]:
xy = np.array([particle_x, particle_y])
xy


Out[43]:
array([[ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
         1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,
         1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,
         1.04,  1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,
         1.02,  1.04,  1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,
         1.  ,  1.02,  1.04,  1.06,  1.08],
       [ 0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,
         0.4 ,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,  0.44,
         0.44,  0.44,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,  0.48,
         0.48,  0.48,  0.48,  0.52,  0.52,  0.52,  0.52,  0.52,  0.52,
         0.52,  0.52,  0.52,  0.52,  0.56,  0.56,  0.56,  0.56,  0.56,
         0.56,  0.56,  0.56,  0.56,  0.56]])

In [58]:
xy[0,:]


Out[58]:
array([ 0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,  1.06,
        1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,  1.04,
        1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,  1.02,
        1.04,  1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,  1.  ,
        1.02,  1.04,  1.06,  1.08,  0.9 ,  0.92,  0.94,  0.96,  0.98,
        1.  ,  1.02,  1.04,  1.06,  1.08])

In [59]:
xy[0,b]


Out[59]:
array([ 0.9 ,  0.92,  0.94,  1.  ,  1.02,  1.04,  1.06,  1.08])

In [60]:
xy[0,:][b]


Out[60]:
array([ 0.9 ,  0.92,  0.94,  1.  ,  1.02,  1.04,  1.06,  1.08])

In [61]:
xy[0,:]b


  File "<ipython-input-61-eed9982554ec>", line 1
    xy[0,:]b
           ^
SyntaxError: invalid syntax

In [64]:
xy[:,b]


Out[64]:
array([[ 0.9 ,  0.92,  0.94,  1.  ,  1.02,  1.04,  1.06,  1.08],
       [ 0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ,  0.4 ]])

In [65]:
xy[b,:]


C:\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 2 but corresponding boolean dimension is 50
  if __name__ == '__main__':
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-65-8b335198ac73> in <module>()
----> 1 xy[b,:]

IndexError: index 2 is out of bounds for axis 0 with size 2

In [ ]: