matlab矩阵和折叠列表
我在mathematica中有两个问题,想在matlab中解决它们:
measure := RandomReal[] - 0.5
m = 10000;
data = Table[measure, {m}];
fig1 = ListPlot[data, PlotStyle -> {PointSize[0.015]}]
Histogram[data]
matlab:
measure =@ (m) rand(1,m)-0.5
m=10000;
for i=1:m
data(:,i)=measure(:,i);
end
figure(1)
plot(data,'b.','MarkerSize',0.015)
figure(2)
hist(data)
它给了我:
???出现以下错误 从 function_handle 转换为 双精度:使用 ==> 时出错双
如果我这样做:
measure =rand()-0.5
m=10000;
data=rand(1,m)-0.5
那么,我在图 1 中得到正确的结果,但在图 2 中 y= 轴是错误的。
另外,如果我在mathematica中有这个:
steps[m_] := Table[2 RandomInteger[] - 1, {m}]
steps[20]
Walk1D[n_] := FoldList[Plus, 0, steps[n]]
LastPoint1D[n_] := Fold[Plus, 0, steps[n]]
ListPlot[Walk1D[10^4]]
我这样做了:
steps = @ (m) 2*randint(1,m,2)-1;
steps(20)
Walk1D =@ (n) cumsum(0:steps(n)) --> this is ok i think
LastPointold1D= @ (n) cumsum(0:steps(n))
LastPoint1D= @ (n) LastPointold1D(end)-->but here i now i must take the last "folding"
Walk1D(10)
LastPoint1D(10000)
plot(Walk1D(10000),'b')
并且我得到一个空矩阵并且没有图..
i have two problems in mathematica and want to do them in matlab:
measure := RandomReal[] - 0.5
m = 10000;
data = Table[measure, {m}];
fig1 = ListPlot[data, PlotStyle -> {PointSize[0.015]}]
Histogram[data]
matlab:
measure =@ (m) rand(1,m)-0.5
m=10000;
for i=1:m
data(:,i)=measure(:,i);
end
figure(1)
plot(data,'b.','MarkerSize',0.015)
figure(2)
hist(data)
And it gives me :
??? The following error occurred
converting from function_handle to
double: Error using ==> double
If i do :
measure =rand()-0.5
m=10000;
data=rand(1,m)-0.5
then, i get the right results in plot1 but in plot 2 the y=axis is wrong.
Also, if i have this in mathematica :
steps[m_] := Table[2 RandomInteger[] - 1, {m}]
steps[20]
Walk1D[n_] := FoldList[Plus, 0, steps[n]]
LastPoint1D[n_] := Fold[Plus, 0, steps[n]]
ListPlot[Walk1D[10^4]]
I did this :
steps = @ (m) 2*randint(1,m,2)-1;
steps(20)
Walk1D =@ (n) cumsum(0:steps(n)) --> this is ok i think
LastPointold1D= @ (n) cumsum(0:steps(n))
LastPoint1D= @ (n) LastPointold1D(end)-->but here i now i must take the last "folding"
Walk1D(10)
LastPoint1D(10000)
plot(Walk1D(10000),'b')
and i get an empty matrix and no plot..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于@Itamar 基本上回答了你的第一个问题,这里是对第二个问题的评论。你几乎做对了。您需要定义,
因为
cumsum
是FoldList[Plus,0,your-list]
的直接模拟。然后,代码中的plot
就可以正常工作了。另请注意,无论是在 Mathematica 还是 Matlab 代码中,都无需单独定义LastPoint1D
- 在这两种情况下,它都是生成的列表(向量)steps< 的最后一个点/代码>。
编辑:
对
LastPoint1D
进行一些扩展:我的猜测是您希望它成为由Walk1D
计算的步行的最后一个点。因此,在我看来,将其作为生成的行走(向量)的函数并返回其最后一个点是有意义的。例如:然后,您将其用作:
HTH
Since @Itamar essentially answered your first question, here is a comment on the second one. You did it almost right. You need to define
since
cumsum
is a direct analog ofFoldList[Plus,0,your-list]
. Then, theplot
in your code works fine. Also, notice that, either in your Mathematica or Matlab code, it is not necessary to defineLastPoint1D
separately - in both cases, it is the last point of your generated list (vector)steps
.EDIT:
Expanding a bit on
LastPoint1D
: my guess is that you want it to be a last point of the walk computed byWalk1D
. Therefore, it would IMO make sense to just make it a function of a generated walk (vector), that returns its last point. For example:Then, you use it as:
HTH
将代码转换为 Matlab 时遇到一些错误/错误:
data = Table[measure, {m}];
行创建了m
的副本measure
,在您的情况下,它将创建一个大小为 (1,m) 的随机向量。如果这是真的,在 Matlab 中它只是data =measure(m);
m
,因此使用矩阵没有任何意义调用它时的符号(:
)。data = Zeros(1,m);
来完成此操作。编辑
关于你的第二个问题,如果你用文字描述你想要实现的目标,而不是尝试阅读你的(产生错误的)代码,会更容易帮助你。明显错误的一件事是使用像
0:steps(n)
这样的表达式,因为您将m:n
与两个标量m
和 < code>n 生成向量,但steps(n)
生成向量,而不是标量。您可能会得到一个空矩阵,因为steps(n)
返回的向量中的第一个值可能是 -1,而0:-1
会生成一个空向量。You have a few errors/mistakes translating your code to Matlab:
data = Table[measure, {m}];
createsm
copies ofmeasure
, which in your case will create a random vector of size (1,m). If that is true, in Matlab it would simply bedata = measure(m);
m
, therefor it makes no sense using a matrix notation (the:
) when calling it.for
loop, it will run much faster if you allocate the matrix in advance, otherwise Matlab will re-allocate memory to resize the matrix in each iteration. You do this bydata = zeros(1,m);
.EDIT
Regarding your 2nd question, it would be easier to help you if you describe in words what you want to achieve, rather than trying to read your (error producing) code. One thing which is clearly wrong is using expression like
0:steps(n)
, since you usem:n
with two scalarsm
andn
to produce a vector, butsteps(n)
produces a vector, not a scalar. You probably get an empty matrix since the first value in the vector returned bysteps(n)
might be -1, and0:-1
produces an empty vector.