2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Matlabのdiff()をPythonで

Posted at

Matlabでちょいちょい使うdiff()
これもpythonではnumpy使ってできる。

#numpy.diff()
matlabでXのDim軸方向のN階の差分はdiff(X,N,Dim)で求める。
同じことは、numpy.diff(X,n=N,axis=Dim)でできる
ただし、matlabのDimは1から始まるのに対し、numpy.diffは0から始まる点に注意。
もっとも、これは配列のインデックスとかと同じ。

#例:diff() in Matlab

x = reshape(0:99,[],5)';
dx = diff(x,1,1);
ddx = diff(x,2,1);
>> x
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
>> dx

dx =

    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20
    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20
    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20
    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20    20
>> ddx

ddx =

     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

#例:numpy.diff() in Python

import numpy as np

x = np.arange(100).reshape(5, -1)
dx = np.diff(x, n=1, axis=0)
ddx = np.diff(x, n=2, axis=0)
>>> 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]]
>>>  print(dx)
[[20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20]
 [20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20]
 [20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20]
 [20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20]]
>>> print(ddx)
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

同じですね。
#余談:列vs行
diff()の話はこれでいいのだが、
何も考えずにベクトルつくると縦だったり横だったりの挙動がMatlabとPython(numpy)で違うっぽい?
(上の例だとMatlabの方でしれっと転置している)
気が向いたら調べるかも。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?