Translate code matlab to python numpy -


i don't understand matlab programming, understand coding in python...

here matrix want solve

matlab code:

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

result :

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 

anyone me please, how solve problem... regards!

my recreation in ipython session (having first tested code in octave session):

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]: 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]]) 

in matlab

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

is closer to

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

but in numpy list append (or in case extend) faster. had initial x appropriate list.

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

i can same values without loop

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.        ]])] 

but it's bit of pain rearrange them order produced iteration, [1, cos, sin, cos, sin].


Comments