You are on page 1of 6

MatlabtoiPythonConversion.

HereI'llbepostingtheMatlabcodesIsawintheDiseoMecatrnico(MechatronicDesignII)course andhowtheycanbetranslatedtoPythoncodewhichtomeisfarbetterasitsfree,opensourceand moreversatile.Soherewego!

A)IntroductiontoprogramminginMatlab.

Code1.
%Matlabcode. %PrintsHelloworldonthescreen. disp("Helloworld!") I n[] : # P y t h o nv e r s i o n . p r i n t ( " H e l l ow o r l d ! " )

Code2Variables.
%Matlabcode. var1=3.14 myString="helloworld" P y t h o nv e r s i o n . I n[] : # v a r 1=3 . 1 4 m y S t r i n g=" h e l l ow o r l d ! " p r i n t ( v a r 1 ) p r i n t ( m y S t r i n g )

Code3Arrays.

%Matlabcode. row=[125.46.6]col=[1234] I n[] : # P y t h o nv e r s i o n . r o w=[ 2 ,2 ,2 ,2 ] p r i n t ( r o w ) # O ra l t e r n a t i v e l yu s i n gt h eN u m p yp a c k a g e . i m p o r tn u m p ya sn p p r i n t ( " C r e a t ear o wa r r a yu s i n gN u m p y " ) r o w=n p . a r r a y ( [ 2 ,2 ,2 ,2 ] ) p r i n t ( r o w ) p r i n t ( " C r e a t eac o l u m na r r a yu s i n gN u m p y " ) c o l=n p . a r r a y ( [ [ 2 ] ,[ 2 ] ,[ 2 ] ,[ 2 ] ] ) p r i n t ( c o l ) # P r i n t st h ed i m e n s i o n so ft h ea r r a y( e l e m e n t s ) . p r i n t ( " D i m e n s i o n so ft h ea r r a y :" ) p r i n t ( c o l . s h a p e ) # P r i n t st h en u m b e ro fi t e m si nt h ea r r a y . p r i n t ( " I t e m si nt h ea r r a y :" ) p r i n t ( c o l . s i z e ) # P r i n t st h ed a t at y p eo ft h ea r r a y . p r i n t ( " D a t at y p e :" ) p r i n t ( c o l . d t y p e ) # D i m e n s i o n s . p r i n t ( " D i m e n s i o n s :" ) p r i n t ( c o l . n d i m )

# T h ea d v a n t a g eo fu s i n gN u m p yo v e rt h es t a n d a r dP y t h o nf a s h i o ni st h a

Code4Matrix.
I n[] : % M a t l a bc o d e . a =[ 12 ; 34 ] ; I n[] : # P y t h o nc o d e . i m p o r tn u m p ya sn p m a t r i x=n p . a r r a y ( [ ( 1 ,0 ,0 ) ,( 0 ,1 ,0 ) ,( 0 ,0 ,1 ) ] ) p r i n t ( m a t r i x )

Code5Specialmathoperations.
%Matlabcode. sqrt(2) log(2),log10(0.23) cos(1.2),atan(.8) exp(2+4*i) round(1.4),floor(3.3),ceil(4.23) angle(i)abs(1+i) P y t h o nc o d e . I n[] : # # T h i sl i b r a r yt a k e sc a r eo fm a t h e m a t i c a lo p e r a t i o n s . i m p o r tc m a t ha sm i m p o r tm a t h # T og e ts q u a r er o o t . p r i n t ( m . s q r t ( 2 ) ) # L o g ( 2 ) p r i n t ( m . l o g ( 2 ) ) # L o g 1 0 ( 0 . 2 3 ) p r i n t ( m . l o g 1 0 ( 0 . 2 3 ) ) # C o s i n ef u n c t i o n . p r i n t ( m . c o s ( 4 5 ) ) # S i n ef u n c t i o n( R A D I A N S ) . p r i n t ( m . s i n ( m . p i / 2 ) ) # E x p o n e n t . p r i n t ( m . e x p ( 2 + 4 * 1 j ) ) # R o u n du p . p r i n t ( m a t h . c e i l ( 4 . 2 3 ) ) # R o u n dd o w n . p r i n t ( m a t h . f l o o r ( 4 . 2 3 ) ) # A b s o l u t ev a l u e . p r i n t ( a b s ( 1 + 1 j ) )

Code6Transpose.
%Matlabcode. a=[1234+i] transpose(a) a' a.' I n[] : # P y t h o nv e r s i o n . i m p o r tn u m p ya sn p a=n p . a r r a y ( [ 1 ,2 ,3 ,4 + 1 j ] ) p r i n t ( a ) b=a . t r a n s p o s e ( ) p r i n t ( b )

Code7Ones,zeros,randomnumbers.
%Matlabcode. o=ones(1,10) z=zeros(23,1) r=rand(1,45) n=nan(1,69) a=linspace(0,10,5) I n[] : # P y t h o nc o d e . i m p o r tn u m p ya sn p # R o wv e c t o rw i t h1 0e l e m e n t sa s1 . o=n p . o n e s ( ( 1 , 1 0 ) ) p r i n t ( o ) # C o l u m nv e c t o rw i t h2 3e l e m e n t sa s0 . z=n p . z e r o s ( ( 2 3 , 1 ) ) p r i n t ( z ) # R a n d o mv e c t o rw i t h4 5e l e m e n t s . f r o mn u m p y . r a n d o mi m p o r t*

r=r a n d ( 1 , 4 5 ) p r i n t ( r ) # E m p t yv e c t o r . N=n p . e m p t y ( ( 1 , 6 9 ) ) p r i n t ( N ) # P r o g r e s s i v ev e c t o r . a=n p . l i n s p a c e (0 ,1 0 ,5) p r i n t ( a )

Code8Plotting.
%Matlabcode. x=linspace(0,4*pi,10) y=sin(x) plot(x,y) I n[ 1 ] : # P y t h o nc o d e . i m p o r tp y l a ba sp l i m p o r tn u m p ya sn p i m p o r tc m a t ha sm x=n p . l i n s p a c e ( 0 ,4 * m . p i ,1 0 0 ) y=n p . s i n ( x ) p l . p l o t ( x , y ) p l . s h o w ( )

Code9Functions.
%Matlabcode. function[x,y,z]=funName(in1,in2) I n[ 2 ] : # P y t h o nv e r s i o n . d e ff u n c t i o n ( ) : p r i n t ( " T h i si saf u n c t i o n " ) f u n c t i o n ( ) T h i si saf u n c t i o n I n[] :

You might also like