2
$\begingroup$

How could I generate the following list of points using some function or mapping without using the table function.

x = (1 - Cos[# Pi])/2 & /@ Range[0, 1, 0.1];
y = (1 - Cos[# Pi])/2 & /@ Range[0, 1, 0.1];

nx = Length[x];
ny = Length[y];

nodesElement = 
      Flatten[Table[{{x[[i]], y[[j]]}, {x[[i + 1]], y[[j]]}, {x[[i + 1]], 
          y[[j + 1]]}, {x[[i]], y[[j + 1]]}}, {j, 1, ny - 1}, {i, 1, 
         nx - 1}], 1];

I am interested in more pure functional programming

$\endgroup$
4
  • 1
    $\begingroup$ One way: Flatten[Outer[{y, x} |-> {{x[[1]], y[[1]]}, {x[[2]], y[[1]]}, {x[[2]], y[[2]]}, {x[[1]], y[[2]]}}, Partition[y, 2, 1], Partition[x, 2, 1], 1], 1] $\endgroup$
    – thorimur
    Commented Jun 24, 2021 at 18:47
  • $\begingroup$ if you instead wanted a different order of the points you could maybe use Tuples instead of a custom function—or maybe there's a slicker way I'm not thinking of! $\endgroup$
    – thorimur
    Commented Jun 24, 2021 at 18:51
  • $\begingroup$ Is the order of your element important? $\endgroup$
    – bRost03
    Commented Jun 24, 2021 at 19:08
  • 1
    $\begingroup$ No. The order of the elements is not important $\endgroup$ Commented Jun 24, 2021 at 19:13

2 Answers 2

3
$\begingroup$

How about this one: Tuples /@ Tuples[Partition[x, 2, 1], 2]

Or in the case $x\neq y$: Tuples /@ Tuples[{Partition[x, 2, 1], Partition[y, 2, 1]}]

Note that this gives the same thing as

Flatten[Table[{{x[[i]], y[[j]]}, {x[[i]], y[[j+1]]}, {x[[i+1]], 
    y[[j]]}, {x[[i+1]], y[[j+1]]}}, {i, 1, ny - 1}, {j, 1, nx - 1}], 1]

Which is just a reordering of what you posted.

$\endgroup$
2
$\begingroup$

Using RotateLeft and MapThread:

xv = Indexed[x, #] & /@ Range[5]
yv = Indexed[y, #] & /@ Range[5]

MapThread[{{#1, #2}, {#3, #2}, {#3, #4}, {#1, #4}} &, {xv, yv, 
  RotateLeft[xv], RotateLeft[yv]}]

That wraps around, you can use Most for a finite set of quads.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.