6
$\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$
2
  • $\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
    Commented Nov 14, 2014 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
    Commented Nov 14, 2014 at 22:21

5 Answers 5

3
$\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$
4
$\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$
3
$\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$
3
$\begingroup$
test = {{1, 1}, {0, 1}, {-1, 1}};
vec = {1, 0, -1};

Using Table and Tuples:

ff[a_, v_] := Sequence @@@ 
              Thread[Table[Flatten /@ Tuples@{{p}, #} &@#, {p, v}]] &@a;

Comparing with the function proposed by @Syed and using FoldList:

(*f = Sequence @@@ Outer[Prepend, #1, vec, 1] &; @Syed's function*)

FoldList[ff[#1, #2] &, test, Array[vec &, 2]] === NestList[f, test, 2]

(*True*)
$\endgroup$
2
$\begingroup$
Clear[f];
test = {{1, 1}, {0, 1}, {-1, 1}};
f = Sequence @@@ Outer[Prepend, #1, {1, 0, -1}, 1] &

NestList[f, test, 2]

$\left\{\left( \begin{array}{cc} 1 & 1 \\ 0 & 1 \\ -1 & 1 \\ \end{array} \right),\left( \begin{array}{ccc} 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 \\ \end{array} \right),\left( \begin{array}{cccc} 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 \\ \end{array} \right)\right\}$

$\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.