MATLAB® for Python® Users
The MATLAB language is designed primarily for math-intensive scientific computing. MATLAB combines a desk-
top environment tuned for iterative analysis with a programming language that expresses matrix and array
mathematics directly. Understanding the philosophy and API design can help while learning MATLAB.
» General Behavior » Referencing
MATLAB
Python MATLAB MATLAB Syntax Purpose Example
Syntax Syntax Purpose Examples
() Index (copy-on-write) x(1,1)
# % Comment %hello
[] Create array x = [1 2 3]
print Do not Print output x
terminate Join arrays z = [x ; y]
with;
/ ... Continue to next x = 1+...2; {} Create cell arrays x = {42; "hello world"}
line
os ! Operating system ! echo hi Extract contents from x{1,1}
command a container
. Access class proper- obj.Data
+ - * / + - * / Mathematical x = 1+2 ty or method
operators Reference table or t.FieldName
** ^ Exponent x = y^2 struct field
• Beginning element has an index of 1.
* / ** .* ./ .^ Element-wise x = [1 2].*
operators [3 4] • Indexing is left and right inclusive.
not, ~ & | NOT, AND, OR if x<2 & x>2 • Indexing options include N-D indexing (row,col), linear
and, or logical operators indexing (element number), and logical indexing (condition-
al statement).
del clear Clear variable clear x y
from memory
clear clc Clear command clc
window
» Functions
Creating functions You can declare functions within a function z = foo(x,y)
function file. Input arguments are ...
captured in parentheses. end
Multiple outputs are captured with function [a,b] = foo(x,y)
square brackets. ...
end
Calling functions with input arguments y = foo(x,y,"Name",Value)
and name-value pairs
mathworks.com
» Data Types » Control Flow
Similar data types:
Statement Example
for for i = 1:10
Python MATLAB ...
float double, single end
if if x<3
complex complex single, complex double
...
int (u)int8, (u)int16, (u)int32, (u)int64 elseif x == 2
else
float(nan) NaN ...
end
float(inf) inf
while while x<3
str str, char ...
end
bool logical
switch-case switch switch _ arg
dict struct ...
case case _ arg
list, tuple cell ...
end
pandas.dataframe table try-catch try
...
MATLAB defaults to store all numeric values as double-precision floating-point catch
numbers. Python stores some numbers as integers and others as floating-point ...
numbers. In MATLAB, for x=4 and y=4.0, x is always equal to y. end
» Objects
Define a class Use a class
classdef MyClass • Save the class definition with the same name as the class
properties MyClass.m
MyProp • Create an object of the class
end a = MyClass
methods • Access the properties
function obj = MyClass(val) a.MyProp
end • Call methods to perform operations
function y = MyMethod(obj,x) b = MyMethod(a,val)
end • To pass-by-reference, create a “handle” class
end classdef myclass < handle
end ...
end
mathworks.com
© 2021 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional trademarks. 9/21
Other product or brand names may be trademarks or registered trademarks of their respective holders.