matlab矢量输出

时间:2013-02-19 18:18:14

标签: function matlab vector output

我尝试使用此代码进行矢量输出。只是一些我需要以矢量形式得到的函数

  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 个答案:

答案 0 :(得分: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