12
$\begingroup$

I'm working with a list of lists (a large table).

How would I extract the first 9 rows from every 22 rows of this list?

Essentially, I'd like to drop rows 10 to 22, and then 32 to 44, and so on, repeated until the end of the data.

For example, I'd like to turn this:

data = {{1,1,1},{2,2,2},{3,3,3},{4,4,4},{5,5,5},{6,6,6},{7,7,7},{8,8,8},
{9,9,9},{10,10,10},{11,11,11},{12,12,12},{13,13,13},{14,14,14},{15,15,15},
{16,16,16},{17,17,17},{18,18,18},{19,19,19},{20,20,20},{21,21,21}, 
{22,22,22},{23,23,23}}

Into:

{{1,1,1},{2,2,2},{3,3,3},{4,4,4},{5,5,5},{6,6,6},{7,7,7},{8,8,8},
{9,9,9},{23,23,23} (*and so on*)}

I am a beginner at Mathametica--please forgive me if this is a repeat question.

Many thanks.

$\endgroup$
14
$\begingroup$

You can use Partition with a suitable offset:

Partition[Range[100], 9, 22]

{{1, 2, 3, 4, 5, 6, 7, 8, 9}, {23, 24, 25, 26, 27, 28, 29, 30, 31}, {45, 46, 47, 48, 49, 50, 51, 52, 53}, {67, 68, 69, 70, 71, 72, 73, 74, 75}, {89, 90, 91, 92, 93, 94, 95, 96, 97}}

As some have pointed out, this solution needs to be slightly modified if "overhang" is desirable. WReach notes that one can use

Partition[data, UpTo[9], 22]

for this case in Mathematica 10.3 and later.

| improve this answer | |
$\endgroup$
  • 8
    $\begingroup$ The complete answer should be Flatten[Partition[data, 9, 22, {1, 1}, {}], 1] $\endgroup$ – swish Jun 24 '17 at 21:15
  • $\begingroup$ To see where are the problems without the ...{1,},{}] try Partition[Range[115], 9, 22,{1,1},{}]and compare to Partition[Range[125], 9, 22] $\endgroup$ – andre314 Jun 24 '17 at 21:21
  • 6
    $\begingroup$ +1. From Mathematica 10.3 onwards we can write Partition[data, UpTo[9], 22] if we want to keep any "dangling" elements at the end of the list. $\endgroup$ – WReach Jun 24 '17 at 21:29
  • $\begingroup$ Works perfectly. Thanks very much C.E., andre, swish, and WReach. $\endgroup$ – musicmaker Jun 24 '17 at 21:31
  • 1
    $\begingroup$ @swish It wasn't clear from the question that there could be overhang, but good point in any case. $\endgroup$ – C. E. Jun 24 '17 at 22:38
9
$\begingroup$

you can do :

ReplacePart[data,{i_ /; Mod[i-1,22]>8}-> Nothing]

{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}, {6, 6, 6}, {7, 7, 7}, {8, 8, 8}, {9, 9, 9}, {23, 23, 23}}

Edit

Here is a more understable syntax, specially for beginners:

Table[If[Mod[i-1,22]>8,Nothing,data[[i]]],{i,1,Length[data]}]
| improve this answer | |
$\endgroup$
5
$\begingroup$
n = 100;
o = 22;

data = Table[{i, i, i}, {i, 100}];

pos = NestList[# + o &, {1, 9}, Floor[n/o]]

{{1, 9}, {23, 31}, {45, 53}, {67, 75}, {89, 97}}

data[[#1 ;; #2]] & @@@ pos // Short (* thanks Mr. Wizard *)

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ OP wanted to extract the rows from data. You have computed the correct indices for the rows but still need to apply it. $\endgroup$ – Jack LaVigne Jun 28 '17 at 0:56
  • $\begingroup$ Thanks - I think now it is correct $\endgroup$ – eldo Jun 28 '17 at 11:14
  • $\begingroup$ For what it's worth data[[# ;; #2]] & @@@ pos is a little bit more concise, but you might as well use Take[data, #] & /@ pos. $\endgroup$ – Mr.Wizard Jun 28 '17 at 12:21
  • $\begingroup$ Updated - overcomplications are a problem everywhere $\endgroup$ – eldo Jun 28 '17 at 12:33
  • $\begingroup$ @eldo You got my vote :) $\endgroup$ – Jack LaVigne Jun 28 '17 at 14:53
5
$\begingroup$
#[[Flatten[Range[Range[9], Length@#, 22], {2,1}]]]& @ data

{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}, {6, 6, 6},
{7, 7, 7}, {8, 8, 8}, {9, 9, 9}, {23, 23, 23}}

| improve this answer | |
$\endgroup$
  • $\begingroup$ It is an order of magnitude faster to use Transpose rather than Sort, i.e. Catenate[Range[Range[9], n, 22]\[Transpose]]. By the way this is essentially f5 from mathematica.stackexchange.com/a/21220/121. I need to compare Transpose and Flatten however. Edit I guess I used Flatten to handle ragged lists. $\endgroup$ – Mr.Wizard Jun 28 '17 at 12:29
  • $\begingroup$ @Mr.Wizard, Catenate[Range[Range[9], n, 22]\[Transpose]] gives an error message (The first two levels of {{1,23},{2,24},{3},{4},{5},{6},{7},{8},{9}} cannot be transposed). I updated with an alternative way to avoid Sorting. $\endgroup$ – kglr Jun 28 '17 at 12:38
  • $\begingroup$ Yeah, I realized that pretty quickly. Now your answer is exactly f5, excluding syntax. $\endgroup$ – Mr.Wizard Jun 28 '17 at 12:39
  • $\begingroup$ @Mr.Wizard, right re f5. Makes me feel proud:) $\endgroup$ – kglr Jun 28 '17 at 12:44
  • $\begingroup$ That's very generous, as you surely know I have learned a lot from you. $\endgroup$ – Mr.Wizard Jun 28 '17 at 13:33

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