从mathematica到matlab的代码——table命令
我有来自mathematica的以下代码,并试图用matlab来做,但我做不到:
tX := Sum[Random[] - 0.5, {m}]/m
m=1;
km=10m;
dataX = Table[tX, {km}]
fig2 = ListPlot[dataX, PlotStyle -> {RGBColor[1, 0, 0], PointSize[0.015]}]
我这样做了:
tx=sum(rand(1,m)-0.5) ./ m;
m=100;
km=100*m;
datax=zeros(tx,1);
for i=1:km
datax(i,1)=[tx];
end
我有两个问题:
- 在mathematica中,tx :=意味着变量tx是每次使用时都会进行评估。 我怎样才能在matlab中完成这个?
- 我的代码中有一些错误,因为当它给我绘图时,它给了我一条直线,但它应该给出大量的点。
I have the following code from mathematica and trying to do it with matlab but I can't do it:
tX := Sum[Random[] - 0.5, {m}]/m
m=1;
km=10m;
dataX = Table[tX, {km}]
fig2 = ListPlot[dataX, PlotStyle -> {RGBColor[1, 0, 0], PointSize[0.015]}]
I did this :
tx=sum(rand(1,m)-0.5) ./ m;
m=100;
km=100*m;
datax=zeros(tx,1);
for i=1:km
datax(i,1)=[tx];
end
I have two problems:
- In mathematica the tx := means that the variable tx is evaluated each time it is used.
How can I accomplish this in matlab? - I have some mistake or mistakes in my code because when it gives me the plot it gives me a straight line but it should give a big number of points.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这就是您想要的:
要生成
tX
的实例,只需输入tX(m)
,其中m
是您想要的值。解释一下:
tX 是一个函数句柄,它相当于 Mathematica 中的 tX[m_] := Sum[RandomReal[],{m}]/m 。
dataX 是用 arrayfun 构造的,它将第一个槽中的函数句柄应用于第二个槽中向量中的每个元素。该命令大致相当于 Mathematica 中的
Table[tX[m],{km}]
。I believe this is what you want:
To generate an instance of
tX
, just typetX(m)
, wherem
is the value you want.to explain some of this:
tX
is a function handle, it is equivalent totX[m_] := Sum[RandomReal[],{m}]/m
in Mathematica.dataX is constructed with
arrayfun
which applies the function handle in the first slot to each element in the vector in the second slot. That command is roughly equivalent toTable[tX[m],{km}]
in Mathematica.