2

How can I convert the following lists to a list of lists?

{
 Flatten[{Range[2, 4, 1]}],    Flatten[{Range[6, 20, 1]}],  
 Flatten[{Range[26, 28, 1]}], Range[30, 31, 1], Range[35, 36, 1],
 Table[
 {
  Flatten[{Range[2 + n, 4 + n, 1]}],    
  Flatten[{Range[6 + n, 20 + n, 1]}],  
  Flatten[{Range[26 + n, 28 + n, 1]}], Range[30 + n, 31 + n, 1], 
  Range[35 + n, 36 + n, 1]
 }, {n, 36, 2304, 36}
   ]
 }

I want to transform the output generated by the above Code to something like, for example,

{{1,2,5}, {6,9,67,56},...,{4,45,67,5,7,7}}

I just want to get rid of unnecessary curly brackets generated.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Tugrul Temel
  • 6,193
  • 3
  • 12
  • 32

3 Answers3

2
Flatten[# + Range[{2, 6, 26, 30, 35}, {4, 20, 28, 31, 36}] & /@ Range[0, 2304, 36], 1]

gives the same output as the method in your answer.

You can also use

Flatten[# + (Range @@@ {{2, 4}, {6, 20}, {26, 28}, {30, 31}, {35, 36}}) & /@ 
  Range[0, 2304, 36], 1]

% == %%

True
kglr
  • 394,356
  • 18
  • 477
  • 896
1

This does what you want, with fewer Flatten (Note that Flatten[{Range[...]}] is the same as Range[...]):

Table[
  {Range[2 + n, 4 + n, 1],
   Range[6 + n, 20 + n, 1],
   Range[26 + n, 28 + n, 1],
   Range[30 + n, 31 + n, 1],
   Range[35 + n, 36 + n, 1]},
  {n, {0} ~ Join ~ Range[36, 2304, 36]}
] ~ Flatten ~ 1

The first part of your lists, generated "by hand" in your code with the explicit Range calls, would have been generated by your Table expression for $n=0$, so I explicitly added that value of $n$ to the Table iterator, in addition to the other values that were already included there.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
  • I wanted to create an index of numbers for aggregation of the selected rows and columns in a matrix. For example, {2,3,4} in the list of lists is used to sum up the elements in the associated rows and columns and maintains the resulting row/column as a new matrix element, while dropping the individual rows/columns summed up. This way matrix dimension is reduced by aggregation. Basically, I wanted to aggregate the selected rows/columns of a matrix. Thanks for your answer. – Tugrul Temel Jan 27 '21 at 12:42
0

I found out that the following does what I want, but I do not like it at all because there are many Flatten commends around. It looks ugly to me. Maybe someone can improve upon this.

Flatten[
 {
  {
   Flatten[{Range[2, 4, 1]}],    Flatten[{Range[6, 20,  1]}],  
Flatten[{Range[26, 28, 1]}], Range[30, 31, 1], Range[35, 36, 1]
 },
 Flatten[
 Table[
{
 Flatten[{Range[2 + n, 4 + n, 1]}],    
 Flatten[{Range[6 + n, 20 + n, 1]}],  
 Flatten[{Range[26 + n, 28 + n, 1]}], Range[30 + n, 31 + n, 1], 
 Range[35 + n, 36 + n, 1]
 }, {n, 36, 2304, 36}
 ], 1]
 }, 1]
Tugrul Temel
  • 6,193
  • 3
  • 12
  • 32