Common | C | C++ | MATLAB | Python | |
Variable arguments | <stdarg.h> T f(...) Packed in va_arg | Very BAD! Cannot overload when signatures are uncertain. | varargin varargout Both packed as cells. MATLAB does not have named arguments | *args (simple, stored as tuples)**kwargs (specify input by keyword, stored as a dictionary) | |
Referencing | N/A | operator[] | (_) is for referencessubsindex [_] is for concat {_} is for (un)pack | __getitem__() | |
Default values | N/A | Supported | Not supported. Manage with inputParser() | Non-intuitive static data behavior. Stick to None or immutables. | |
Major Dimension | Row | Row | Column | Row (Native/Numpy) Column for Pandas | |
Constness | const | const | Only in classes | N/A (Consenting adults) | |
Variable Aliasing | Pointers | References | NO! Copy-on-write Handle classes under limited circumstances | References | |
= assignment | Copy one element | Values: Copy References: Bind | New Copy Copy-on-write | NO VALUES Bind references only (could be to unnamed objects) | |
Chained access operators | N/A | Difficult to operator overload it right | Difficult to get it right. MATLAB had some chaining bugs with dataset() as well. | Chains correctly natively | |
Assignment expressions (assignment evaluates to assigned lvalue) | = | = | N/A | Named Expression := | |
Version Management | verLessThan() isMATLABReleaseOlderThan | virtenv (Virtual Environment) | |||
Stream (Conveyor belt mechanism. Saves memory) | I/O (std, file, sockets) | iterator inSTL containers | MATLAB doesn’t do references. Just increment indices. | iterators (uni-directional only)iter(): __iter__() next(): __next__() | |
Generators | Input iterators | output only per iteration with yield | |||
Coroutines | C++20 <coroutine> |
Data Types
Common | C | C++ | MATLAB | Python |
Sets | N/A | std::set | Only set operations, not set data type | { , , ...} |
Dictionaries | std::unordered_map | – Dynamic fieldnames (qualified varnames as keys) – containers.Map() or dictionary() since R2022b | Dictionaries{key:value} (Native) | |
Heterogeneous containers | cells {} | lists (mutable) tuples (immutable) | ||
Structured Heterogeneous containers | table() dataset() [Old]Mix in classes | Pandas Dataframe | ||
Array, Matrices & Tensors | Native [ , ; , ] | Numpy/PyTorch | ||
Records | struct | class (members) | dynamic field (structs) properties (class) getfield()/setfield() | No structs (use dicts) attribute (class) getattr()/setattr() |
Type deduction | N/A | auto | Native | Native |
Type extraction | N/A | decltype() for compile time (static)typeid() for RTTI (runtime) | class() | type() |
orderly-set
package.Array Operations
Common | MATLAB | Python | ||
Repeat | repmat() | [] * N np.repeat() | ||
Logical Indexing | Native | List comprehension Boolean Indexing (Numpy) |
Editor Syntax
Common | C | C++ | MATLAB | Python | ||
Commenting | /* ... */ // (only for newer C) | // (single line)/* ... */ (block) | % (single line)(Block): %{ | # (single line)""" or ''' is docstring which might be undersirably picked up | ||
Reliable multi-line commenting (IDE) | Ctrl+(Shift)+R (Windows), / (Mac or Linux) | [Spyder]: Ctrl+ 1 (toggle), 4 (comment), 5 (uncomment) | ||||
Code cell (IDE) | %% | [Spyder]: # %% | ||||
Line Continuation | \ | \ | ... | \ | ||
Console Precision | format | %precision (IPython) | ||||
Clear variables | clear / clearvars | %reset -sf (IPython) |
Python is messy about the workspace, so if you just delete
Object Oriented Programming Constructs
Common | C++ | MATLAB | Python | ||
Getters Setters | No native syntax. Name mangle (prefix or suffix) yourself to manage | Define methods:get.x set.x | Getter:@property Setter: @x.setter | ||
Deleters | Members can’t be changed on the fly | Members can’t be changed on the fly | Deleter (removing attributes dynamically by del ) | ||
Overloading (Dispatch function by signature) | Overloading | Overload only by first argument | @overload (Static type)
| ||
Initializing class variables | Initializer Lists Constructor | Constructor | Constructor | ||
Constructor | ClassName() | ClassName() | __init__() | ||
Destructor | ~ClassName() | delete() | __del__() | ||
Special methods | Special member functions | (no name) method that control specific behaviors | Magic/Dunder methods | ||
Operator overloading | operator | operator methods to define | Dunder methods | ||
Resource Self-cleanup | RIAA | onCleanup() : make a dummy object with cleanup operation as destructor to be removed when it goes out of scope | with Context Managers |
setattr()
, which includes methods. MATLAB’s dynamicprops allows adding properties (data members) on the fly with addproponCleanup() does not work reliably on Python because MATLAB
Functional Programming Constructs
Common | C++ | MATLAB | Python | ||
Function as variable | Functors (Function Objects) operator() | Function Handle | Callables (Function Objects) __call__() | ||
Lambda Syntax | Lambda[capture](inputs) {expr} -> optional trailing return type | Anonymous Function@(inputs) expr | Lambdalambda inputs: expr | ||
Closure (Early binding): an instance of function objects | Capture [] only as necessary.Early binding [=] is capture all. | Early binding ONLY for anonymous functions (lambda). Late binding for function handles to loose or nested functions. | Late binding* by default, even for Lambdas. Can capture Po through default valueslambda x,P=Po: x+P (We’re relying users to not enter the captured/optional input argument) |
An instance of a function object is not a closure if there’s any parameter that’s late bound. All lambdas (anonymous functions) in MATLAB are early bound (at creation).
The more proper way (without creating an extra optional argument that’s not supposed to be used, aka defaults overridden) to convert late binding to early binding (by capturing variables) is called partial application, where you freeze the parameters (to be captured) by making them inputs to an outer layer function and return a function object (could be lambda) that uses these parameters.
The same trick (partial application) applies to bind (capture) variables in simple/nested function handles in MATLAB which do behave the same way (early binding) like anonymous functions (lambda).
Currying is partial application one parameter at a time, which is tedious way to stay faithful to pure functional programming.
Coroutines / Asynchronous Programming
MATLAB natively does not support coroutines.
Common | C++20 | Python |
Generators | Input Iterators | Functions that yield instead of return(Implicitly return a generator/functor with iter and next ) |