LoginSignup
0
1

More than 5 years have passed since last update.

MATLAB: repmat(inpx, [1 3]) > Numpy: np.tile(inpx, (1,3))

Last updated at Posted at 2017-11-19
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)

MATLAB

>> inpx = [ 3 1 4; 1 5 9;]

inpx =

     3     1     4
     1     5     9

>> repmat(inpx, [1 3])

ans =

     3     1     4     3     1     4     3     1     4
     1     5     9     1     5     9     1     5     9

>> 

上記はrepmat(inps, 1, 3)と同じ意味になる。

B = repmat(A,r1,...,rN) specifies a list of scalars, r1,..,rN, that describes how copies of A are arranged in each dimension.

関連
https://jp.mathworks.com/help/matlab/learn_matlab/array-indexing.html?lang=en

Numpy

Numpyでどう実装するか。

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.repeat.html
というのはある。
試したが、思うようには処理できなかった。

[Python]繰り返しパターンの行列を作る方法(repmat/tile)
にて方法が掲載されている。

情報感謝です。

test_repmat_171119.py
import numpy as np

inpx = [[3,1,4],[1,5,9]]
print(inpx)

print("---case1---")
wrkx = np.repeat(inpx, 3, axis=0)
print(wrkx)

print("---case2a---")
wrkx = np.repeat(inpx, [3,3,3], axis=1)
print(wrkx)

print("---case2b---")
wrkx = np.tile(inpx, (1,3))
print(wrkx)

run
$ python3 test_repmat_171119.py 
[[3, 1, 4], [1, 5, 9]]
---case1---
[[3 1 4]
 [3 1 4]
 [3 1 4]
 [1 5 9]
 [1 5 9]
 [1 5 9]]
---case2a---
[[3 3 3 1 1 1 4 4 4]
 [1 1 1 5 5 5 9 9 9]]
---case2b---
[[3 1 4 3 1 4 3 1 4]
 [1 5 9 1 5 9 1 5 9]]

np.tile()を使うことで、MATLABのrepmatと同じ結果が得られました。

numpy.matlib.repmatの方は"No module named numpy.matlab"となったため、環境構築が必要になりそうです(今回は使いません)。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1