Matlab等同于Python的“无". [英] The Matlab equivalent of Python's "None"
问题描述
Matlab中是否有一个关键字与python中的None
大致相同?
Is there a keyword in Matlab that is roughly equivalent to None
in python?
我正在尝试使用它来标记函数的可选参数.我正在翻译以下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)
进入Matlab
function rtrn = f(x,y)
if y == []:
rtrn = g(x);
else
rtrn = h(x,y);
end;
end
如您所见,当前我正在将[]
用作None
.有更好的方法吗?
As you can see currently I am using []
as None
. Is there a better way to do this?
推荐答案
在您的特定情况下.您可以使用nargin
确定在调用函数时此处提供了多少个输入参数.
in your specific case. you may use nargin
to determine how many input arguments here provided when calling the function.
摘自 MATLAB文档:
nargin和nargout函数 使您能够确定有多少输入 并输出一个函数的参数 叫.然后,您可以使用 要执行的条件语句 根据不同的任务 参数数量.例如,
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.
这篇关于Matlab等同于Python的“无".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!