2
$\begingroup$

I have a table of elements that generate a list

q[i_]=i;
B=Table[q[i],{i,1,50}]

I would like to parse the list meaning, take the first 10 elements, then take every other element from 11 to 20, then take every 3rd element from 21 to 50.

Any suggestions? Thank you all

$\endgroup$
3
$\begingroup$
x = Table[i, {i, 1, 50}];

{
  Take[x, 10],
  Take[x, {11, 20, 2}],
  Take[x, {21, 50, 3}]
} // Flatten

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48}

Or as oneliner

Take[x, #] & /@ {{1, 10, 1}, {11, 20, 2}, {21, 50, 3}} // Flatten

Same output

| improve this answer | |
$\endgroup$
  • $\begingroup$ @user2558894 You're welcome $\endgroup$ – eldo Sep 23 '14 at 15:38
2
$\begingroup$
x = Array[q, {50}];

In addition to Take suggested in eldo's answer , you can also use Part (x[[..]])

Join @@ x[[#]] & @@@ {{;; 10}, {11 ;; 20 ;; 2}, {21 ;; 50 ;; 3}}
(* {q[1], q[2],q[3],q[4],q[5],q[6],q[7],q[8],q[9],q[10],
    q[11],q[13],q[15],q[17],q[19],
    q[21],q[24],q[27],q[30],q[33],q[36],q[39],q[42],q[45], q[48]} *)

Or Extract

Join @@ Extract[x, {{Range[10]}, {11 ;; 20 ;; 2}, {21 ;; 50 ;; 3}}]
(* same output *)

See also: Undocumented form of Extract

| improve this answer | |
$\endgroup$
  • $\begingroup$ Interestingly, the undocumented Extract does not work anymore in 10.0.1 $\endgroup$ – RunnyKine Sep 23 '14 at 16:33
  • $\begingroup$ Thank you @RunnyKine, i was just about check that now:) $\endgroup$ – kglr Sep 23 '14 at 16:39
  • $\begingroup$ @RunnyKine, just tried in v10; it still works exactly as in v9. Note that the extraction list should be prepended with something that does not trigger an error message: {{}}, or {0} or {{101,4,3}}, the remainining elements can contain Spans. $\endgroup$ – kglr Sep 23 '14 at 16:44
  • $\begingroup$ I know it works in v10. But it doesn't work in 10.0.1 $\endgroup$ – RunnyKine Sep 23 '14 at 16:45
  • $\begingroup$ @RunnyKine, I see.. I am using Wolfram Programming Cloud. I assumed it runs 10.0.1; perhaps not. $\endgroup$ – kglr Sep 23 '14 at 16:48

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.