matlab矩阵和折叠列表

发布于 2024-10-17 07:04:00 字数 1223 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2024-10-24 07:04:00

由于@Itamar 基本上回答了你的第一个问题,这里是对第二个问题的评论。你几乎做对了。您需要定义,

Walk1D = @ (n) cumsum(steps(n));

因为 cumsumFoldList[Plus,0,your-list] 的直接模拟。然后,代码中的 plot 就可以正常工作了。另请注意,无论是在 Mathematica 还是 Matlab 代码中,都无需单独定义 LastPoint1D - 在这两种情况下,它都是生成的列表(向量)steps< 的最后一个点/代码>。

编辑:

LastPoint1D 进行一些扩展:我的猜测是您希望它成为由 Walk1D 计算的步行的最后一个点。因此,在我看来,将其作为生成的行走(向量)的函数并返回其最后一个点是有意义的。例如:

lastPoint1D = @(walk) (walk(end));

然后,您将其用作:

walk = Walk1D(10000);
lastPoint1D(walk)

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

Walk1D = @ (n) cumsum(steps(n));

since cumsum is a direct analog of FoldList[Plus,0,your-list]. Then, the plot in your code works fine. Also, notice that, either in your Mathematica or Matlab code, it is not necessary to define LastPoint1D 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 by Walk1D. Therefore, it would IMO make sense to just make it a function of a generated walk (vector), that returns its last point. For example:

lastPoint1D = @(walk) (walk(end));

Then, you use it as:

walk = Walk1D(10000);
lastPoint1D(walk)

HTH

旧伤慢歌 2024-10-24 07:04:00

将代码转换为 Matlab 时遇到一些错误/错误:

  • 如果我没记错的话,data = Table[measure, {m}]; 行创建了 m 的副本measure,在您的情况下,它将创建一个大小为 (1,m) 的随机向量。如果这是真的,在 Matlab 中它只是 data =measure(m);
  • 您定义的函数获取单个参数 m,因此使用矩阵没有任何意义调用它时的符号(:)。
  • 顺便说一句,如果你在 for 循环中将数据插入到矩阵中,如果你提前分配矩阵,它会运行得更快,否则 Matlab 将重新分配内存来调整矩阵的大小在每次迭代中。您可以通过data = Zeros(1,m);来完成此操作。
  • “在图 2 中 y= 轴是错误的”是什么意思?你期望它是什么?

编辑

关于你的第二个问题,如果你用文字描述你想要实现的目标,而不是尝试阅读你的(产生错误的)代码,会更容易帮助你。明显错误的一件事是使用像 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:

  • If I am not wrong, the line data = Table[measure, {m}]; creates m copies of measure, which in your case will create a random vector of size (1,m). If that is true, in Matlab it would simply be data = measure(m);
  • The function you define gets a single argument m, therefor it makes no sense using a matrix notation (the :) when calling it.
  • Just as a side-note, if you insert data into a matrix inside a 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 by data = zeros(1,m);.
  • What do you mean by "in plot 2 the y=axis is wrong"? What do you expect it to be?

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 use m:n with two scalars m and n to produce a vector, but steps(n) produces a vector, not a scalar. You probably get an empty matrix since the first value in the vector returned by steps(n) might be -1, and 0:-1 produces an empty vector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文