[Python] How to make a matrix of repeating patterns (repmat / tile)

How to make a matrix of repeating patterns. When you want to do so-called repmat in Matlab

There are two methods.

  1. Use numpy.matlib.repmat
  2. Use numpy.tile

1. How to use numpy.matlib.repmat

numpy.matlib.repmat(a, m, n)

In [1]: import numpy as np

In [2]: import numpy.matlib

In [3]: np.matlib.repmat((1,2,3),1,5)
Out[3]: array([[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]])

In [4]: A = np.diag((100,200,300))

In [5]: print A
[[100   0   0]
 [  0 200   0]
 [  0   0 300]]

In [6]: np.matlib.repmat(A,2,2)
Out[6]: 
array([[100,   0,   0, 100,   0,   0],
       [  0, 200,   0,   0, 200,   0],
       [  0,   0, 300,   0,   0, 300],
       [100,   0,   0, 100,   0,   0],
       [  0, 200,   0,   0, 200,   0],
       [  0,   0, 300,   0,   0, 300]])

In [7]: np.matlib.repmat(A,1,2)
Out[7]: 
array([[100,   0,   0, 100,   0,   0],
       [  0, 200,   0,   0, 200,   0],
       [  0,   0, 300,   0,   0, 300]])

In [8]: np.matlib.repmat(A,2,1)
Out[8]: 
array([[100,   0,   0],
       [  0, 200,   0],
       [  0,   0, 300],
       [100,   0,   0],
       [  0, 200,   0],
       [  0,   0, 300]])

2. How to use numpy.tile

numpy.tile(a, reps)

In [9]: np.tile((1,2,3),(1,5))
Out[9]: array([[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]])

In [10]: np.tile(A,(2,2))
Out[10]: 
array([[100,   0,   0, 100,   0,   0],
       [  0, 200,   0,   0, 200,   0],
       [  0,   0, 300,   0,   0, 300],
       [100,   0,   0, 100,   0,   0],
       [  0, 200,   0,   0, 200,   0],
       [  0,   0, 300,   0,   0, 300]])

In [11]: np.tile(A,(1,2))
Out[11]: 
array([[100,   0,   0, 100,   0,   0],
       [  0, 200,   0,   0, 200,   0],
       [  0,   0, 300,   0,   0, 300]])

In [12]: np.tile(A,(2,1))
Out[12]: 
array([[100,   0,   0],
       [  0, 200,   0],
       [  0,   0, 300],
       [100,   0,   0],
       [  0, 200,   0],
       [  0,   0, 300]])

If you use repmat, for example, you can use it when you want to leave only even-numbered positions in a two-dimensional array and set the rest to 0.

In [27]: X = np.arange(100).reshape(10,10)

In [28]: Y = np.matlib.repmat(np.array([[1,0],[0,0]]),5,5)

In [29]: print X
[[ 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]
 [50 51 52 53 54 55 56 57 58 59]
 [60 61 62 63 64 65 66 67 68 69]
 [70 71 72 73 74 75 76 77 78 79]
 [80 81 82 83 84 85 86 87 88 89]
 [90 91 92 93 94 95 96 97 98 99]]

In [30]: print Y
[[1 0 1 0 1 0 1 0 1 0]
 [0 0 0 0 0 0 0 0 0 0]
 [1 0 1 0 1 0 1 0 1 0]
 [0 0 0 0 0 0 0 0 0 0]
 [1 0 1 0 1 0 1 0 1 0]
 [0 0 0 0 0 0 0 0 0 0]
 [1 0 1 0 1 0 1 0 1 0]
 [0 0 0 0 0 0 0 0 0 0]
 [1 0 1 0 1 0 1 0 1 0]
 [0 0 0 0 0 0 0 0 0 0]]

In [31]: Z = X * Y

In [32]: print Z
[[ 0  0  2  0  4  0  6  0  8  0]
 [ 0  0  0  0  0  0  0  0  0  0]
 [20  0 22  0 24  0 26  0 28  0]
 [ 0  0  0  0  0  0  0  0  0  0]
 [40  0 42  0 44  0 46  0 48  0]
 [ 0  0  0  0  0  0  0  0  0  0]
 [60  0 62  0 64  0 66  0 68  0]
 [ 0  0  0  0  0  0  0  0  0  0]
 [80  0 82  0 84  0 86  0 88  0]
 [ 0  0  0  0  0  0  0  0  0  0]]

In the case of a color image, it is three-dimensional data, so it is as follows.

ipython


In [1]: from skimage import data

In [2]: from skimage import io

In [3]: import numpy as np

In [4]: sz = 48

In [5]: image = data.lena()

In [6]: patch1 = image[250:250+sz,250:250+sz,:]

In [7]: patch1.shape
Out[7]: (48, 48, 3)

In [8]: mask = np.tile(np.array([[[1],[0]],[[0],[0]]],dtype=np.uint8),(sz/2,sz/2,3))

In [9]: mask.shape
Out[9]: (48, 48, 3)

In [10]: print mask
[[[1 1 1]
  [0 0 0]
  [1 1 1]
  ..., 
  [0 0 0]
  [1 1 1]
  [0 0 0]]


 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ..., 
  [0 0 0]
  [0 0 0]
  [0 0 0]]]

In [11]: patch2 = patch1 * mask

In [12]: patch12 = np.hstack((patch1,patch2))

20160104_6.png

Recommended Posts

[Python] How to make a matrix of repeating patterns (repmat / tile)
[Python] How to make a list of character strings character by character
[Python] How to make a class iterable
How to write a list / dictionary type of Python3
How to make a Python package using VS Code
[Python] How to make an adjacency matrix / adjacency list [Graph theory]
How to shuffle a part of a Python list (at random.shuffle)
How to make a Python package (written for an intern)
How to get a list of built-in exceptions in python
How to make a Japanese-English translation
How to write a Python class
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make a Blender plugin
How to make a crawler --Basic
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
How to determine the existence of a selenium element in Python
[Python] How to force a method of a subclass to do something specific
How to make a string into an array or an array into a string in Python
How to check the memory size of a variable in Python
I tried to make a regular expression of "time" using Python
How to make a surveillance camera (Security Camera) with Opencv and Python
I tried to make a regular expression of "date" using Python
How to check the memory size of a dictionary in Python
Slack --APIGateway --Lambda (Python) --How to make a RedShift interactive app
[Python] Summary of how to use pandas
Try to make a kernel of Jupyter
Make a relation diagram of Python module
[Python] How to invert a character string
[Python2.7] Summary of how to use unittest
[Python2.7] Summary of how to use subprocess
How to run a Maya Python script
[Question] How to use plot_surface of python
[Python] How to extract / delete / convert a matrix containing missing values (NaN)
How to send a visualization image of data created in Python to Typetalk
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Python] How to read a csv file (read_csv method of pandas module)
How to format a list of dictionaries (or instances) well in Python
How to make a dialogue system dedicated to beginners
How to calculate the volatility of a brand
How to read a CSV file with Python 2/3
[Python] How to use two types of type ()
How to create a Python virtual environment (venv)
How to open a web browser from python
How to clear tuples in a list (Python)
How to embed a variable in a python string
Summary of how to import files in Python 3
How to create a JSON file in Python
How to make a dictionary with a hierarchical structure.
I want to make a game with Python
Make a copy of the list in Python
Summary of how to use MNIST in Python
How to add a Python module search path
How to make a QGIS plugin (package generation)
How to specify attributes with Mock of python
How to notify a Discord channel in Python
How to get dictionary type elements of Python 2.7
I read "How to make a hacking lab"
[Blender x Python] How to make an animation