3
$\begingroup$

I start with an array such as {{1,1},{0,1},{-1,1}}. (1)

I want to create an array from this which replaces each array inside the array with 3 new arrays, one prepended with 1, one prepended with 0 and one prepended with -1. For example, applying this to the above array would give:

{{1,1,1},{0,1,1},{-1,1,1},{1,0,1},{0,0,1},{-1,0,1},{1,-1,1},{0,-1,1},{-1,-1,1}} (2)

I need to find an efficient method to apply this multiple times to an array (i.e. the same algorithm would then be applied to (2) yielding a list containing 27 lists and so on).

I have tried using For loops but cannot get them to work and I suspect they would be very slow.

Ideally I would also like to be able to specify how many times to apply this algorithm.

$\endgroup$
  • $\begingroup$ In the above text, the (1) and (2) are meant as labels for equations, rather than something to do with the actual problem. $\endgroup$ – Jack Nov 14 '14 at 21:50
  • $\begingroup$ Please hold on with an accept (a day or two), let's do not discourage others. Better answers may appear :) I'm glad it helps. $\endgroup$ – Kuba Nov 14 '14 at 22:21
1
$\begingroup$
(Flatten /@ Reverse /@ Tuples[{#, {-1, 0, 1}}]) & @ {{1, 1}, {0, 1}, {-1, 1}}
{{-1, 1, 1}, {0, 1, 1}, {1, 1, 1}, {-1, 0, 1}, {0, 0, 1}, {1, 0, 1},
 {-1, -1, 1}, {0, -1, 1}, {1, -1, 1}}
Nest[ (Flatten /@ Reverse /@ Tuples[{#, {-1, 0, 1}}]) & , 
      {{1, 1}, {0, 1}, {-1, 1}},
      2]
{{-1, -1, 1, 1}, {0, -1, 1, 1}, {1, -1, 1, 1}, {-1, 0, 1, 1}, {0, 0, 
 1, 1}, {1, 0, 1, 1}, {-1, 1, 1, 1}, {0, 1, 1, 1}, {1, 1, 1, 
1}, {-1, -1, 0, 1}, {0, -1, 0, 1}, {1, -1, 0, 1}, {-1, 0, 0, 1}, {0,
 0, 0, 1}, {1, 0, 0, 1}, {-1, 1, 0, 1}, {0, 1, 0, 1}, {1, 1, 0, 
1}, {-1, -1, -1, 1}, {0, -1, -1, 1}, {1, -1, -1, 1}, {-1, 0, -1, 
1}, {0, 0, -1, 1}, {1, 0, -1, 1}, {-1, 1, -1, 1}, {0, 1, -1, 1}, {1,
1, -1, 1}}
$\endgroup$
1
$\begingroup$

This is a very literal way of doing precisely what you described:

f[arr_] := Sequence[Prepend[arr, 1], Prepend[arr, 0], Prepend[arr, -1]]

f /@ {{1, 1}, {0, 1}, {-1, 1}}

You can think of Sequence[a,b,c] as representing a,b,c without any other expression surrounding them.

$\endgroup$
0
$\begingroup$

Just for variety:

f[s_] := ({#, Sequence @@ s} & /@ Range[-1, 1]);
g[lst_] := Join @@ (f /@ lst);

So,

test = {{1, 1}, {0, 1}, {-1, 1}};
g[test]

gives:

(*{{-1, 1, 1}, {0, 1, 1}, {1, 1, 1}, {-1, 0, 1}, {0, 0, 1}, {1, 0, 
  1}, {-1, -1, 1}, {0, -1, 1}, {1, -1, 1}}*)

and can be nested:

Nest[g, test, 2]

gives:

(*{{-1, -1, 1, 1}, {0, -1, 1, 1}, {1, -1, 1, 1}, {-1, 0, 1, 1}, {0, 0, 
  1, 1}, {1, 0, 1, 1}, {-1, 1, 1, 1}, {0, 1, 1, 1}, {1, 1, 1, 
  1}, {-1, -1, 0, 1}, {0, -1, 0, 1}, {1, -1, 0, 1}, {-1, 0, 0, 1}, {0,
   0, 0, 1}, {1, 0, 0, 1}, {-1, 1, 0, 1}, {0, 1, 0, 1}, {1, 1, 0, 
  1}, {-1, -1, -1, 1}, {0, -1, -1, 1}, {1, -1, -1, 1}, {-1, 0, -1, 
  1}, {0, 0, -1, 1}, {1, 0, -1, 1}, {-1, 1, -1, 1}, {0, 1, -1, 1}, {1,
   1, -1, 1}}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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