如何在mathematica中以矩阵的形式跟踪路径

时间:2012-01-11 00:02:44

标签: wolfram-mathematica

我有一个矩阵,即一个非粗糙的列表列表,并给出一个坐标列表,例如以{{0,0},{1,1},{2,2},...{5,5}}的形式,我想跟踪该矩阵中的路径并以图形方式显示结果。路径的彩色带很好。

请帮我在 Mathematica 中编写这样的功能。非常感谢!

3 个答案:

答案 0 :(得分:15)

这是一种可能性。

pos = {{1, 1}, {1, 2}, {2, 2}, {3, 3},
  {3, 4}, {3, 5}, {4, 5}, {5, 5}};
mat = HankelMatrix[8];
display = Map[Pane[#,{16,20},Alignment->Center]&, mat, {2}];
display = MapAt[Style[#, Background -> Yellow]&, display, pos];
Grid[display, Spacings->{0,0}]

enter image description here

如您所述,用管子勾画条目更难。但是,如果我们愿意降级为图形基元,那么可以这样做。

mat = IdentityMatrix[8];
pos = {{1, 1}, {1, 2}, {2, 2}, {3, 3},
  {3, 4}, {3, 5}, {4, 5}, {5, 5}};
pos = Map[{#[[1]], -#[[2]]} &, pos];
outline = {CapForm["Round"], JoinForm["Round"],
  {AbsoluteThickness[30], Line[pos]},
  {AbsoluteThickness[28], White, Line[pos]}};
disks = Table[{Darker[Yellow, 0.07], Disk[p, 0.25]}, 
  {p, pos}];
numbers = MapIndexed[Style[Text[#, {#2[[1]], -#2[[2]]}, 
  {-0.2, 0.2}], FontSize -> 12] &, mat, {2}];
Graphics[{outline, disks, numbers}, ImageSize -> 300]

enter image description here

答案 1 :(得分:10)

另一种可能性,使用ItemStyle

m = RandomInteger[10, {10, 10}];
c = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {5, 6}, {5, 7}, {4, 8}};
Grid[m, ItemStyle -> {Automatic, Automatic, Table[i -> {16, Red}, {i, c}]}]

最终看起来像这样:

Mathematica graphics

答案 2 :(得分:8)

我可能误解了这个问题,但这是我认为你要求的:

coords = Join @@ Array[List, {3, 4}]
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {3, 
  1}, {3, 2}, {3, 3}, {3, 4}}

path = RandomSample[coords, Length[coords]]
{{1, 2}, {3, 3}, {2, 2}, {2, 4}, {3, 1}, {1, 4}, {1, 3}, {2, 1}, {3, 
  4}, {3, 2}, {2, 3}, {1, 1}}

labels = Text[StyleForm[#], #] & /@ coords;


Graphics[Line[path], Epilog -> labels]

enter image description here