Skip to main content

13.6) Gluing And Repeating


You can concatenate (eg glue together) two 1-D arrays into a new one:

>>> a = np.arange(8)
>>> b = np.arange(7)
>>> c= np.concatenate((a,b))

Notice here that the concatenate function has one argument which is a Tuple of the arrays to concatenate. This is why we have double parentheses.

Repeat array elements and return a new array:

>>> np.repeat(a,4)

If you manipulate two dimensional arrays of data with x and y (or latitude and longitudes) coordinate vectors, it can be useful to make the x and y coordinate vectors into 2D arrays of the same shape as A. You can do this with the meshgrid function:

>>> x = np.arange(8)
>>> y = np.arange(5)
>>> (XX,YY)= np.meshgrid(x,y)

XX and YY are two arrays of ny (5) rows and nx (8) columns. XX is the x coordinate at each location of the 2-D grid and YY the y coordinate at each location.

Similarly you could replace x by longitude and y by latitude.