Code from mathematica to matlab -- table command
I have the following code from mathematica and trying to do it with matlab but I can't do it:
tX := Sum[Rando开发者_Python百科m[] - 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.
I believe this is what you want:
m = 1;
km = 10*m;
tX = @(m) sum( rand(m,1) - 0.5 )/ m;
dataX = arrayfun(tX, m * ones(km,1));
plot(1:km, dataX, 'r.')
To generate an instance of tX
, just type tX(m)
, where m
is the value you want.
to explain some of this:
tX
is a function handle, it is equivalent to tX[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 to Table[tX[m],{km}]
in Mathematica.
精彩评论