7
$\begingroup$

I'd like to remove the first element from each vector and adding it to the beginning of each subsequent element within the vector so that:

{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}

Becomes:

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}

I'd like to use >select< with the condition that the length is greater than 1 to get rid of the first elements after I have flattened the vector lists to the first level. However, how would I alter the vectors using the join command?

rogerRoger = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4,
     15}, {9, 21}}};

Select[Flatten[Join[], 1], Length@# > 1 &]
$\endgroup$

4 Answers 4

5
$\begingroup$

{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}

Becomes:

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}

Can you not use If ?

lis ={{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9,21}}};
If[Length[#[[1]]] == 1, z = #[[1]]; Join[z, #] & /@ Rest[#], #] & /@ lis
Flatten[%, 1]

Mathematica graphics

with the condition that length is greater than 1

I am not sure I understand why the above? Length of what?

You want to join the first list to the rest if the first list had length 1, right? That is what you example does.

So if first rest had length say 2, (greater than 1) it will not touch that list and will leave it as is. This is what I understood you wanted.

lis={{{1,3},{2,5},{5,10},{10,23}},{{2},{3,35},{4,15},{9,21}}};
If[Length[#[[1]]]==1,z=#[[1]];Join[z,#]&/@Rest[#],#]&/@lis
Flatten[%,1]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thanks that was really useful, as for the condition length greater than one it's the criteria and everything within flatten is the list for >select[list, criteria]< which is what I wanted yes. $\endgroup$
    – user85376
    Mar 3, 2022 at 2:18
7
$\begingroup$

Cases can be used.

list = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}};

Flatten[Cases[list, {x_, y__} :> (Join[x, #] & /@ {y})], 1]

(* {1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}} *)
$\endgroup$
6
$\begingroup$
lst = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}};

Join @@ Map[Flatten /@ Thread[{#[[1, 1]], Rest @ #}] &] @ lst
{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}

Also

Join @@ (Prepend[#[[1]]] /@ {##2} & @@@ lst)

Join @@ (Prepend @@@ Tuples[{{##2}, #}] & @@@ lst)

Flatten[Join @@@ Tuples[{{#}, {##2}}] & @@@ lst, 1]

Distribute[{{#}, {##2}}, List, List, Sequence, Join] & @@@ lst

all give

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}

And ... for fun:

☺☺☺ = ## & @@ (♯ |-> ## & @@@ {#, ♯}) /@ {##2} & @@@ # &;

☺☺☺ @ lst
{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9,    21}}
$\endgroup$
5
$\begingroup$
list = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}};

Define a helper function,

join[a_List, b_List] := Join[a, #] & /@ b

Then,

list2 = Flatten[join[#[[1]], Rest@#] & /@ list, 1]

(* {1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}} *)
$\endgroup$

Your Answer

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