绘制 for 循环值(数组)
Plotting of for loop values (array)
由于我是 Mathematica 的新手,我遇到了以下问题:
我应该 "fly" y 方向(向上)的无人机,在它撞到位于 {0,5} 的 "obstacle" 之前,它应该移出x 方向的方式。这行得通,但是现在我应该绘制无人机的 "flightpath"。我用一个数组试过这个,但我无法绘制它。有人可以帮我吗?
Reap[For[it = 1, it < 11, it++, drone = {0, it};
Sow[drone] If[obstacle == drone + {0, 1},
For[i = 1, i < 11, i++, drone = {i, it}; Sow[drone]]]]]
`{Null, {{{0, 1}, {0, 2}, {0, 3}, {0, 4}, {1, 4}, {2, 4}, {3, 4}, {4,
4}, {5, 4}, {6, 4}, {7, 4}, {8, 4}, {9, 4}, {10, 4}, {0, 5}, {0,
6}, {0, 7}, {0, 8}, {0, 9}, {0, 10}}}}`
我知道 "For" 不是在 Mathematica 中执行此操作的最佳方法,但我已经习惯了其他程序语言。
为此,我在谷歌上搜索了很多不同的方法(表格、列表等),但 none 成功了,这是我最接近于适合我的解决方案(如果我只能绘制它)。
编辑:
感谢您的解决方案。成功了!
你应该在 Sow[drone]
和 If[obstacle == drone + {0, 1}
之间有一个分号,尽管在这种情况下它仍然有效。这里有一些绘图建议。
obstacles = {{0, 5}, {3, 12}};
i = 0;
path = Reap[For[it = 1, it < 21, it++,
drone = {i, it};
Sow[drone];
If[MemberQ[obstacles, drone + {0, 1}],
Do[drone = {i++, it};
Sow[drone], 3]]]][[2, 1]];
plot = Show[ListLinePlot[path, PlotMarkers -> Automatic],
ListPlot[obstacles, PlotStyle -> Red, PlotMarkers -> {Automatic, 12}],
Frame -> True, PlotRangePadding -> {0.6, {1, 2}}, Axes -> False]
由于我是 Mathematica 的新手,我遇到了以下问题:
我应该 "fly" y 方向(向上)的无人机,在它撞到位于 {0,5} 的 "obstacle" 之前,它应该移出x 方向的方式。这行得通,但是现在我应该绘制无人机的 "flightpath"。我用一个数组试过这个,但我无法绘制它。有人可以帮我吗?
Reap[For[it = 1, it < 11, it++, drone = {0, it};
Sow[drone] If[obstacle == drone + {0, 1},
For[i = 1, i < 11, i++, drone = {i, it}; Sow[drone]]]]]
`{Null, {{{0, 1}, {0, 2}, {0, 3}, {0, 4}, {1, 4}, {2, 4}, {3, 4}, {4,
4}, {5, 4}, {6, 4}, {7, 4}, {8, 4}, {9, 4}, {10, 4}, {0, 5}, {0,
6}, {0, 7}, {0, 8}, {0, 9}, {0, 10}}}}`
我知道 "For" 不是在 Mathematica 中执行此操作的最佳方法,但我已经习惯了其他程序语言。
为此,我在谷歌上搜索了很多不同的方法(表格、列表等),但 none 成功了,这是我最接近于适合我的解决方案(如果我只能绘制它)。
编辑:
感谢您的解决方案。成功了!
你应该在 Sow[drone]
和 If[obstacle == drone + {0, 1}
之间有一个分号,尽管在这种情况下它仍然有效。这里有一些绘图建议。
obstacles = {{0, 5}, {3, 12}};
i = 0;
path = Reap[For[it = 1, it < 21, it++,
drone = {i, it};
Sow[drone];
If[MemberQ[obstacles, drone + {0, 1}],
Do[drone = {i++, it};
Sow[drone], 3]]]][[2, 1]];
plot = Show[ListLinePlot[path, PlotMarkers -> Automatic],
ListPlot[obstacles, PlotStyle -> Red, PlotMarkers -> {Automatic, 12}],
Frame -> True, PlotRangePadding -> {0.6, {1, 2}}, Axes -> False]