matlab 中插入附录,从mathematica到matlab的转换->(附录)

icon1.gif 从mathematica到matlab的转换->(附录)

我在mathematica中有以下内容,想在matlab中使用它。我试过,但是我有错误,无法解决。这是我还没有获得matlab哲学!所以,

intMC = {}; sigmat = {}; Do[np1 = np + i*100; xpoints = Table[RandomReal[], {z1, 1, np1}]; a1t = Table[f[xpoints[[i2]]], {i2, 1, np1}]; a12 = StandardDeviation[a1t]/Sqrt[Length[a1t]]; AppendTo[intMC, {np1, Mean[a1t], a12}]; AppendTo[sigmat, {np1, a12}], {i, 1, ntr}]; 我这样做:

fx=@ (x) exp(-x.^2); intmc=zeros(); sigmat=zeros(); for i=1:ntr np1=np+i*100; xpoints=randn(1,np1); for k=1:np1 a1t=fx(xpoints(k)) end %--> until here it prints the results,but in the %end it gives % me a message " Attempted to access xpoints(2,:); %index out of bounds because size(xpoints)=[1,200] %and stops executing. %a1t=fx(xpoints(k,:)) % -->I tried this instead of the above but %a1t=bsxfun(@plus,k,1:ntr) % it doesn't work a12=std(a1t)/sqrt(length(a1t)) intmc=intmc([np1 mean(a1t) a12],:) %--> i can't handle these 3 and sigmat=sigmat([np1 a12 ],:) %as i said it stopped executing end

回答:

为了将标量附加到Matlab数组,可以调用array(end+1) = value或array = [array;value] (如果要使用1-by-n数组,请用逗号替换分号)。后者也适用于追加数组。要将数组与前者追加在一起,如果要沿第一个维度进行分类,则可以调用array(end+1:end:size(newArray,1),:) = newArray 。

但是,在Matlab中附加执行超过100次迭代的循环是一个不好的主意,因为它很慢。您最好先预先分配数组-甚至更好,对计算进行向量化,这样就根本不需要循环。

如果我对您的理解正确,则希望通过正态分布中越来越多的样本来计算平均值和SEM。这是通过循环执行此操作的方法:

intmc = zeros(ntr,3); %# stores, on each row, np1, mean, SEM sigmat = zeros(ntr,2); %# stores, on each row, np1 and SEM for i=1:ntr %# draw np+100*i normally distributed random values np1 = np+i*100; xpoints = randn(np1,1); %# if you want to use uniform random values (as in randomreal), use rand %# Also, you can apply f(x) directly on the array xpoints %# caculate mean, sem m = mean(xpoints); sem = std(xpoints)/sqrt(np1); %# store intmc(i,:) = [np1, m, sem]; sigmat(i,:) = [np1,sem]; end %# loop over i

更多&回答...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值