poster
2019-12-10, 20:30
Matlab R2009b引入了一个新的“运算符”-〜- (http://blogs.mathworks.com/loren/2009/09/11/matlab-release-2009b-best-new-feature-or/)表示未使用的函数输出或输入。我对此实施有详细的疑问。 (调用所有@Loren (https://stackoverflow.com/users/113700/loren) 。)
对于未使用的输入参数的值,函数会看到什么?
即如果我的功能被定义为
myfunc(argOne, argTwo, argThree) 它这样称呼:
myfunc('arg', ~, 'arg') nargin是2还是3?是argTwo未定义还是为空或其他?
谢谢
回答:
~语法仅适用于函数定义的输入,不适用于函数调用的输入(如本文档页面上所述 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#br67dkp-1) )。换句话说, 这是可以的:
function myfunc(argOne, ~, argThree) %# Will do nothing with the second input %# Do stuff here end 但这不是:
myfunc('arg', ~, 'arg'); %# Error city ;) 因此,在调用函数时,只能在左侧使用~ :
[~, I] = sort([2 4 1 2 5 3]); %# Sort the vector and keep only the index
更多&回答... (https://stackoverflow.com/questions/2002542)
对于未使用的输入参数的值,函数会看到什么?
即如果我的功能被定义为
myfunc(argOne, argTwo, argThree) 它这样称呼:
myfunc('arg', ~, 'arg') nargin是2还是3?是argTwo未定义还是为空或其他?
谢谢
回答:
~语法仅适用于函数定义的输入,不适用于函数调用的输入(如本文档页面上所述 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#br67dkp-1) )。换句话说, 这是可以的:
function myfunc(argOne, ~, argThree) %# Will do nothing with the second input %# Do stuff here end 但这不是:
myfunc('arg', ~, 'arg'); %# Error city ;) 因此,在调用函数时,只能在左侧使用~ :
[~, I] = sort([2 4 1 2 5 3]); %# Sort the vector and keep only the index
更多&回答... (https://stackoverflow.com/questions/2002542)