Matlab-如何构建函数
Matlab有函数Legendre,它返回一个整数m和一个长度为m+1的向量实数rMatlab-如何构建函数,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
现在我想定义另一个函数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});