MATLAB Python Julia Cheatsheet
MATLAB Python Julia Cheatsheet
(https://github.com/QuantEcon/QuantEcon.cheatsheet)
MATLAB–Python–Julia cheatsheet¶
In the Python code we assume that you have already run import numpy as np
In the Julia, we assume you are using v1.0.2 or later with Compat v1.3.0 or later and have run
using LinearAlgebra, Statistics, Compat
Creating Vectors¶
/
MATLAB PYTHON JULIA
A = [1 2 3] A = np.array([1, 2, A = [1 2 3]
3]).reshape(1, 3)
or
A = [1, 2, 3]
Creating Matrices¶
Create a matrix
2 x 2 matrix of zeros
2 x 2 matrix of ones
2 x 2 identity matrix
Diagonal matrix
/
MATLAB PYTHON JULIA
Sparse Matrices
Tridiagonal Matrices
Transpose
/
MATLAB PYTHON JULIA
Concatenate horizontally
Concatenate vertically
Flip left/right
Flip up/down
Repeat matrix (3 times in the row dimension, 4 times in the column dimension)
/
MATLAB PYTHON JULIA
Preallocating/Similar
/
MATLAB PYTHON JULIA
Remove a row
Diagonals of matrix
Mathematical Operations¶
Dot product
A ⋅ B # \cdot<TAB>
/
MATLAB PYTHON JULIA
Matrix multiplication
A * B A @ B A * B
Element-wise multiplication
A .* B A * B A .* B
Matrix to a power
Inverse
or or
A^(-1) A^(-1)
Determinant
Euclidean norm
/
MATLAB PYTHON JULIA
Programming¶
/
MATLAB PYTHON JULIA
Comment one line
Comment block
%{ # Block #=
Comment block # comment Comment block
%} # following PEP8 =#
For loop
While loop
If
If / else
/
MATLAB PYTHON JULIA
x = 10 x = 10 x = 10
fprintf('x = %d \n', x) print(f'x = {x}') println("x = $x")
Function: anonymous
Function
Tuples
/
MATLAB PYTHON JULIA
Closures
function f!(out, x)
out .= x.^2
end
x = rand(10)
y = similar(x)
f!(y, x)
Credits
This cheat sheet was created by Victoria Gregory (https://github.com/vgregory757), Andrij Stachurski (http://drdrij.com/),
Natasha Watkins (https://github.com/natashawatkins) and other collaborators on behalf of QuantEcon
(http://quantecon.org/).