2013-02-19 154 views
0

那麼我試着用這個代碼做矢量輸出。只是一些功能,這導致我需要在向量形式matlab矢量輸出

function [smthOut] = myErf(start, wstep, final) 

    for k = start:wstep:final 
     error_func = 2/sqrt(pi) * erfTaylor(k); 
     smthOut(final/wstep) = error_func; 
     disp(['ON x=', num2str(k), ' RES IS f= ', num2str(error_func)]); 
    end; 

    end 

獲得,但輸出看起來像這樣

>> s = myErf(0, 0.2, 2) 
ON x=0 RES IS f= 0 
ON x=0.2 RES IS f= -0.0029732 
ON x=0.4 RES IS f= -0.022959 
ON x=0.6 RES IS f= -0.073171 
ON x=0.8 RES IS f= -0.1606 
ON x=1 RES IS f= -0.28568 
ON x=1.2 RES IS f= -0.44374 
ON x=1.4 RES IS f= -0.62745 
ON x=1.6 RES IS f= -0.82906 
ON x=1.8 RES IS f= -1.042 
ON x=2 RES IS f= -1.2614 

s = 

    Columns 1 through 8 

     0   0   0   0   0   0   0   0 

    Columns 9 through 10 

     0 -1.2614 

什麼是撥錯?如何解決它?謝謝!

回答

1

由於final/wstep是恆定的(2/0.2 = 10),在循環的每次迭代中,數組的相同字段被結果填充(即,smthOut(10))。

你可以嘗試像

function [smthOut] = myErf(start, wstep, final) 

    cnt = 1; 
    for k = start:wstep:final 
     error_func = 2/sqrt(pi) * erfTaylor(k); 
     smthOut(cnt) = error_func; 
     disp(['ON x=', num2str(k), ' RES IS f= ', num2str(error_func)]); 
     cnt = cnt + 1; 
    end; 

end 
+0

哦,謝謝!該死的,我沒認出來! :\ – DanilGholtsman 2013-02-19 18:35:05