TransWikia.com

Problems with creating a matrix with functions

Mathematica Asked on August 10, 2021

I have a function which gives me all the coordinates of a certain bloc in a lattice (with the help of another function for rows and columns):

(*
  len = length of line
x0,y0 = position of first site
lc: orientation ("r"=row, "c"=colomn)
*)
line[len_, x0_: 0, y0_: 0, rc_: "r"] := Block[{linelist, i},

  If[rc == "r",
   linelist = Table[{rx, y0}, {rx, x0, x0 + len - 1}];
   ,
   linelist = Table[{x0, ry}, {ry, y0, y0 + len - 1}];
   ];

  linelist

  ]

(*
  len(i) = length of bloc in i direction
x0,y0 = position of first site (down-left corner)
*)
bloc[lenx_, leny_, x0_: 0, y0_: 0] := Block[{bloclist},

  bloclist = {};

  For[i = y0, i < y0 + leny, i++,
   Block[{},
    bloclist = Level[Append[bloclist, line[lenx, x0, i, "r"]], {-2}];
    ]
   ];

  bloclist

  ]

The problem is, when I try do make this table

Table[bloc[a, i, 0, 7], {i, 1, 1}, {a, 1, 2}]

it gives me

{{{{0, 7}}, {{0, 7}, {1, 7}, {0, 8}, {1, 8}, {0, 9}, {1, 9}, {0, 
10}, {1, 10}, {0, 11}, {1, 11}, {0, 12}, {1, 12}, {0, 13}, {1, 
13}, {0, 14}, {1, 14}}}}

instead of

{{{{0,7}},{{0,7},{1,7}}}}

Where do all those other terms come from? How can I fix this?

(which is a simplified version of what I must do):

2 Answers

I figured it out, it was simply because I already defined the "i" in the bloc function and used it after to calculate my table. It worked when I changed it.

Answered by Alex on August 10, 2021

At any rate, changing up the functions a little gives the expected result.

line[len_, x0_: 0, y0_: 0, "r"] := Table[{rx, y0}, {rx, x0, x0 + len - 1}]
line[len_, x0_: 0, y0_: 0, rc_?Except["r"]] := Table[{rx, y0}, {rx, x0, x0 + len - 1}]
bloc[lenx_, leny_, x0_: 0, y0_: 0] := Block[{i = y0}, Nest[Level[Append[#, line[lenx, x0, i++, "r"]], {-2}] &, {}, leny]]

Table[bloc[a, i, 0, 7], {i, 1, 1}, {a, 1, 2}]

{{{{0, 7}}, {{0, 7}, {1, 7}}}}

Answered by NonDairyNeutrino on August 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP