将代码matlab转换为python numpy

时间:2016-09-02 05:38:59

标签: python matlab numpy math computer-science

我不了解matlab编程,我只是理解python中的编码......

here my matrix want to solve

Matlab代码:

 x = ones(3,1).
con = [0.505868045540458,0.523598775598299].
series = [1,2,3] 
for j = 1:2 
   #here stuck to translate Python code#
   x = [x cos(con(j)*series)' sin(con(j)*series)'];
end

结果:

1.0000    0.8748    0.4846    0.8660    0.5000
1.0000    0.5304    0.8478    0.5000    0.8660
1.0000    0.0532    0.9986   -0.0000    1.0000

有人帮帮我,如何解决这个问题...... 此致!

2 个答案:

答案 0 :(得分:1)

我在Ipython会话中的重新开始(首次在Octave会话中测试了您的代码):

In [649]: con = np.array([0.505868, 0.5235897])
In [650]: series = np.array([1,2,3])
In [651]: x = [np.ones((3,))]
In [652]: for j in range(2):
     ...:     x.extend([np.cos(con[j]*series), np.sin(con[j]*series)])
     ...:     
In [653]: x
Out[653]: 
[array([ 1.,  1.,  1.]),
 array([ 0.8747542 ,  0.53038982,  0.05316725]),
 array([ 0.48456691,  0.84775388,  0.99858562]),
 array([  8.66029942e-01,   5.00015719e-01,   2.72267949e-05]),
 array([ 0.49999214,  0.86601633,  1.        ])]
In [654]: np.array(x).T
Out[654]: 
array([[  1.00000000e+00,   8.74754200e-01,   4.84566909e-01,
          8.66029942e-01,   4.99992140e-01],
       [  1.00000000e+00,   5.30389821e-01,   8.47753878e-01,
          5.00015719e-01,   8.66016328e-01],
       [  1.00000000e+00,   5.31672464e-02,   9.98585622e-01,
          2.72267949e-05,   1.00000000e+00]])

在MATLAB中

x = [x cos(...) sin(...)]

更接近

x = np.concatenate([x, cos(...), sin(...)], axis=?)

但是在numpy list append(或者在这种情况下是extend)更快。我只需将x初始化到相应的列表中。

==================

我可以在没有循环的情况下获得相同的值

In [663]: y = con[:,None]*series
In [664]: [np.cos(y), np.sin(y)]
Out[664]: 
[array([[  8.74754200e-01,   5.30389821e-01,   5.31672464e-02],
        [  8.66029942e-01,   5.00015719e-01,   2.72267949e-05]]),
 array([[ 0.48456691,  0.84775388,  0.99858562],
        [ 0.49999214,  0.86601633,  1.        ]])]

但是将它们重新排列为迭代产生的顺序[1, cos, sin, cos, sin]会有点痛苦。

答案 1 :(得分:0)

这里有一个解决方案,可以完全删除调整数组大小的循环,适用于大len(con) 应该比matlab解决方案更有效,并通过关联hpaulj&#39 ; s直接翻译 - 这在len(con)中是线性的而不是二次的。

import numpy as np

# declare the arrays
con = np.array([0.505868045540458, 0.523598775598299])
series = np.array([1,2,3])

# use broadcasting to generate trig_arg[i,j] = series[i]*con[j]
trig_arg = series[:,np.newaxis] * con

# Add another dimension that is either cos or sin
trig_elems = np.stack([np.cos(trig_arg), np.sin(trig_arg)], axis=-1)

# flatten out the last two dimensions
all_but_ones = trig_elems.reshape(trig_elems.shape[:-2] + (-1,))

# and add the first column of ones
result = np.concatenate([
    np.ones(series.shape)[:,np.newaxis],
    all_but_ones
], axis=-1)

沿途看看每一步:

# configure numpy output to make it easier to see what's happening
>>> np.set_printoptions(suppress=True, precision=4)
>>> trig_arg
array([[ 0.5059,  0.5236],
       [ 1.0117,  1.0472],
       [ 1.5176,  1.5708]])
>>> trig_elems
array([[[ 0.8748,  0.4846],
        [ 0.866 ,  0.5   ]],

       [[ 0.5304,  0.8478],
        [ 0.5   ,  0.866 ]],

       [[ 0.0532,  0.9986],
        [-0.    ,  1.    ]]])

>>> all_but_ones
array([[ 0.8748,  0.4846,  0.866 ,  0.5   ],
       [ 0.5304,  0.8478,  0.5   ,  0.866 ],
       [ 0.0532,  0.9986, -0.    ,  1.    ]])

>>> result
array([[ 1.    ,  0.8748,  0.4846,  0.866 ,  0.5   ],
       [ 1.    ,  0.5304,  0.8478,  0.5   ,  0.866 ],
       [ 1.    ,  0.0532,  0.9986, -0.    ,  1.    ]])

np.stack相对较新,但可以使用np.concatenate和一些np.newaxis切片进行模拟。或者,您可以直接进入numpy源代码,并将stack的新实现复制到项目中,如果您遇到旧版本的话。