Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab-如何构建函数_Matlab_Function_Math - Fatal编程技术网

Matlab-如何构建函数

Matlab-如何构建函数,matlab,function,math,Matlab,Function,Math,Matlab有函数Legendre,它返回一个整数m和一个长度为m+1的向量实数r 现在我想定义另一个函数fun,它每固定m只给我这个向量的第一个分量,所以我想定义一个函数fun(m,r),它给我向量legendre(m,x)的第一个分量。关键是乐趣(m,r)也应该是一种功能,就像勒让德一样。有人知道怎么做吗?定义函数如下: function out = fun(n,x) temp = legendre(n,x); %// store output of "legendre" in a temp

Matlab有函数Legendre,它返回一个整数m和一个长度为m+1的向量实数r


现在我想定义另一个函数fun,它每固定m只给我这个向量的第一个分量,所以我想定义一个函数fun(m,r),它给我向量legendre(m,x)的第一个分量。关键是乐趣(m,r)也应该是一种功能,就像勒让德一样。有人知道怎么做吗?

定义函数如下:

function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
fun = @(n,x) getfield(legendre(n,x), {1});
当然,这应该放在Matlab路径中的文件
fun.m


或者,如果您有感觉,您可以使用

getfield(legendre(n,x), {1})
直接提取
legendre(n,x)
的第一个元素(没有临时变量)。这允许定义
fun
,如下所示:

function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
fun = @(n,x) getfield(legendre(n,x), {1});

定义函数如下:

function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
fun = @(n,x) getfield(legendre(n,x), {1});
当然,这应该放在Matlab路径中的文件
fun.m


或者,如果您有感觉,您可以使用

getfield(legendre(n,x), {1})
直接提取
legendre(n,x)
的第一个元素(没有临时变量)。这允许定义
fun
,如下所示:

function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
fun = @(n,x) getfield(legendre(n,x), {1});

定义函数如下:

function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
fun = @(n,x) getfield(legendre(n,x), {1});
当然,这应该放在Matlab路径中的文件
fun.m


或者,如果您有感觉,您可以使用

getfield(legendre(n,x), {1})
直接提取
legendre(n,x)
的第一个元素(没有临时变量)。这允许定义
fun
,如下所示:

function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
fun = @(n,x) getfield(legendre(n,x), {1});

定义函数如下:

function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
fun = @(n,x) getfield(legendre(n,x), {1});
当然,这应该放在Matlab路径中的文件
fun.m


或者,如果您有感觉,您可以使用

getfield(legendre(n,x), {1})
直接提取
legendre(n,x)
的第一个元素(没有临时变量)。这允许定义
fun
,如下所示:

function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
fun = @(n,x) getfield(legendre(n,x), {1});