循环以在mathematica中生成矩阵的迭代行

时间:2014-08-27 05:36:51

标签: matrix wolfram-mathematica

我有一个矩阵

FT= {{0, 0, 0}, {1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 2, 1}, {1, 2, 2}, {1,
   2, 3}, {1, 3, 1}, {1, 3, 2}, {1, 3, 3}, {2, 1, 1}, {2, 1, 2}, {2, 
  1, 3}, {2, 2, 1}, {2, 2, 2}, {2, 2, 3}, {2, 3, 1}, {2, 3, 2}, {2, 3,
   3}, {2, 4, 1}, {2, 4, 2}, {2, 4, 3}, {2, 5, 1}, {2, 5, 2}, {2, 5, 
  3}, {2, 6, 1}, {2, 6, 2}, {2, 6, 3}, {3, 1, 1}, {3, 1, 2}, {3, 1, 
  3}, {3, 2, 1}, {3, 2, 2}, {3, 2, 3}, {3, 3, 1}, {3, 3, 2}, {3, 3, 
  3}, {3, 4, 1}, {3, 4, 2}, {3, 4, 3}, {3, 5, 1}, {3, 5, 2}, {3, 5, 
  3}, {3, 6, 1}, {3, 6, 2}, {3, 6, 3}, {3, 7, 1}, {3, 7, 2}, {3, 7, 
  3}, {3, 8, 1}, {3, 8, 2}, {3, 8, 3}, {3, 9, 1}, {3, 9, 2}, {3, 9, 
  3}, {3, 10, 1}, {3, 10, 2}, {3, 10, 3}, {3, 11, 1}, {3, 11, 2}, {3, 
  11, 3}, {3, 12, 1}, {3, 12, 2}, {3, 12, 3}} 

其中每一行代表一个元素的地址,第一个元素给出G,第二个元素给出B,第三个元素给出M.对于第3个元素,G是1,B是1,M是2,依此类推。现在我需要为每个元素生成一个矩阵,这样每当我的G为0或1时,它给我{0,0,0},而对于G> 1,它给我行G-1,G-2等等直到它给出1;对于B,它给出了一个检查,只要B是偶数,它返回B / 2,每当B为奇数时,它返回(B + 1)/ 2,M每次取值3,最后一行{0,0,0} 。对于例如对于FT = {3,12,3}的最后一个元素,它应该给我{{2,6,3},{1,3,3},{0,0,0}}和FT= {2,5,1}它应该给我{{1,3,3}, {0,0,0}}。 能帮我在mathematica中为此编写代码吗? 在此先感谢:)

1 个答案:

答案 0 :(得分:0)

对于此演示,使用小写符号来避免与内置函数冲突,例如: DEIN

此功能定义基本操作

op[{g_, b_, m_}] := {g - 1, If[EvenQ[b], b/2, (b + 1)/2], 3}

应该重复应用,直到满足条件。 NestWhileList对此很有用,例如

NestWhileList[op[#] &, {3, 12, 3}, First[#] > 1 &]
  

{{3,12,3},{2,6,3},{1,3,3}}

另一个函数使用上面的g > 1: -

f[{g_, b_, m_}] := Append[
  If[g > 1, Rest@NestWhileList[op, {g, b, m}, First[#] > 1 &],
   {}], {0, 0, 0}]

E.g。

f[{2, 5, 1}]
  

{{1,3,3},{0,0,0}}

现在f可以映射到ft: -

ft = {
   {0, 0, 0}, {1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 2, 1},
   {1, 2, 2}, {1, 2, 3}, {1, 3, 1}, {1, 3, 2}, {1, 3, 3},
   {2, 1, 1}, {2, 1, 2}, {2, 1, 3}, {2, 2, 1}, {2, 2, 2},
   {2, 2, 3}, {2, 3, 1}, {2, 3, 2}, {2, 3, 3}, {2, 4, 1},
   {2, 4, 2}, {2, 4, 3}, {2, 5, 1}, {2, 5, 2}, {2, 5, 3},
   {2, 6, 1}, {2, 6, 2}, {2, 6, 3}, {3, 1, 1}, {3, 1, 2},
   {3, 1, 3}, {3, 2, 1}, {3, 2, 2}, {3, 2, 3}, {3, 3, 1},
   {3, 3, 2}, {3, 3, 3}, {3, 4, 1}, {3, 4, 2}, {3, 4, 3},
   {3, 5, 1}, {3, 5, 2}, {3, 5, 3}, {3, 6, 1}, {3, 6, 2},
   {3, 6, 3}, {3, 7, 1}, {3, 7, 2}, {3, 7, 3}, {3, 8, 1},
   {3, 8, 2}, {3, 8, 3}, {3, 9, 1}, {3, 9, 2}, {3, 9, 3},
   {3, 10, 1}, {3, 10, 2}, {3, 10, 3}, {3, 11, 1}, {3, 11, 2},
   {3, 11, 3}, {3, 12, 1}, {3, 12, 2}, {3, 12, 3}};    

matrix = Map[f, ft]