2014-09-22 35 views
0

Matlab具有一個返回一個整數m和實數函數的Legendre的R長度爲m + 1的向量。Matlab的 - 如何建立功能

現在我想定義另一個函數fun,它給了我每個固定m只有這個向量的第一個分量,所以我想定義一個函數fun(m,r),它給了我向量legendre的第一個分量M,X)。關鍵是,fun(m,r)也應該是一個函數,就像legendre一樣。有人知道怎麼做這個嗎?

回答

3

定義功能如下:

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.m Matlab的路徑中。


或者,如果你感覺hackish,可以使用

getfield(legendre(n,x), {1}) 

提取的legendre(n,x)直接在第一元件(沒有臨時變量)。這允許將fun定義爲anonymous function,如下所示:

fun = @(n,x) getfield(legendre(n,x), {1});