2013-05-09 46 views
0

我想創建一個函數(symfun),並且我想把它分成個案,即如果t>那麼答案將是a,如果t答案將會是b。 事情是,該matlab不會讓我把一個if語句後sym函數。爲函數im定義了不同的情況matlab(symfun)

>> l = symfun(0, [m]); 
>> l(m) = if m>0 3 

還我試圖創建一個功能:

function [x1] = xt_otot_q3(t) 

,並試圖將兩種功能之間的連接:

>> l(m) = xt_otot_q3(m) 
Conversion to logical from sym is not possible. 

有沒有什麼辦法,打破了symfun爲個案?

回答

0

不知道我明白你想要什麼。 此代碼「結合」的功能symfun和XT + otot_q3定義如下:

function test; 
    m=randn(4);    % N(0,1) random numbers 
    l=xtotot_q3(symfun(0,m)) % combine xt_otot_q3 and symfun 
    l=symfun(0,xtotot_q3(m)) % combine symfun and xt_otot_q3 
return 

function lval=symfun(thr,rval); 
    lval=ones(size(rval)); % output, size of input, = 1 
    lval(rval<thr)=-1;  % output = -1 if input < thr 
return 

function lval=xtotot_q3(rval); 
    lval=3*rval+1;   % some function, in this case 3 * input + 1 
return 

您可以保存整個位爲test.m,然後從MATLAB提示撥打test。也許如果你從這開始,那麼你可以修改它以適應你的需求。

+0

嗨,我確實保存並運行它,問題是我沒有得到任何功能後,我運行它...我的意思是沒有我可以評估的Lval ...你知道這是爲什麼嗎? – 2013-05-11 10:31:57

+0

您可以將第一行更改爲'function l = test;' - 如果您在matlab中鍵入'test',則第二個'l'將返回爲'ans'。或者如果你輸入'l = test',它會返回爲'l'。 – 2013-05-12 08:40:22

+0

而且,如果你想要第一個'l'而不是第二個,你可以改變以'l = ...'開始的兩行的順序。 – 2013-05-12 08:41:57