Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Matlab equivalent of Python's "None"

Tags:

python

matlab

Is there a keyword in Matlab that is roughly equivalent to None in python?

I am trying to use it to mark an optional argument to a function. I am translating the following Python code

def f(x,y=None):
    if y == None:
        return g(x)
    else:
        return h(x,y)

into Matlab

function rtrn = f(x,y)
    if y == []:
        rtrn = g(x);
    else
        rtrn = h(x,y);
    end;
end

As you can see currently I am using [] as None. Is there a better way to do this?

like image 385
D R Avatar asked Nov 15 '09 13:11

D R


People also ask

How similar is MATLAB to Python?

The biggest technical difference between MATLAB and Python is that in MATLAB, everything is treated as an array, while in Python everything is a more general object. For instance, in MATLAB, strings are arrays of characters or arrays of strings, while in Python, strings have their own type of object called str .

Is there a null in MATLAB?

Use the null function to calculate orthonormal and rational basis vectors for the null space of a matrix.

Is NumPy similar to MATLAB?

MATLAB® and NumPy have a lot in common, but NumPy was created to work with Python, not to be a MATLAB clone. This guide will help MATLAB users get started with NumPy.

Is MATLAB harder than Python?

MATLAB has very strong mathematical calculation ability, Python is difficult to do. Python has no matrix support, but the NumPy library can be achieved. MATLAB is particularly good at signal processing, image processing, in which Python is not strong, and performance is also much worse.


2 Answers

in your specific case. you may use nargin to determine how many input arguments here provided when calling the function.

from the MATLAB documentation:

The nargin and nargout functions enable you to determine how many input and output arguments a function is called with. You can then use conditional statements to perform different tasks depending on the number of arguments. For example,

function c = testarg1(a, b) 
     if (nargin == 1)
         c = a .^ 2; 
     elseif (nargin == 2)
         c = a + b; 
     end

Given a single input argument, this function squares the input value. Given two inputs, it adds them together.

like image 76
Adrien Plisson Avatar answered Sep 18 '22 06:09

Adrien Plisson


NaN while not equivalent, often serves the similar purpose.

like image 40
SilentGhost Avatar answered Sep 20 '22 06:09

SilentGhost