5
$\begingroup$

I have a list as follows:

lis1={{{a1,b1,c1,d1,e1},{a2,b2,c2,d2,e2}},
{{a3,b3,c3,d3,e3},{a4,b4,c4,d4,e4}}}

and would like to delete certain elements from it resp. pick certain elements to get a list as follows

lis2= {{{a1,c1,e1},{a2,c2,e2}},
    {{a3,c3,e3},{a4,c4,e4}}}

My problem is that the data I would like to select are not in an consecutive order (otherwise I could use the Part function). Does anyone have an hint?

$\endgroup$
4
$\begingroup$

You actually can still use the Part function!

If you know the positions explicitly you can try:

lis1[[All, All, {1, 3, 5}]]

Or, if they follow a regular pattern:

lis1[[All, All, 1 ;; 5 ;; 2]]
$\endgroup$
7
$\begingroup$

Just learned from this answer by ciao that Downsample is made for this kind of task:

Downsample[#, {1, 2}] & /@ lis1
(* {{{a1, c1, e1}, {a2, c2, e2}}, {{a3, c3, e3}, {a4, c4, e4}}} *)

You can also use Take, Drop and Extract to get the same result:

Take[#, All, {1, -1, 2}] & /@ lis1
Drop[#, {}, {2, -1, 2}] & /@ lis1
First@Rest@Extract[lis1, {{0}, {;; , ;; , ;; ;; 2}}]

You can use arbitrary lists for Part specifications with Part and Extract. For example

#[[All, ;; , {1, 4, 5}]] &@lis1  (* or *)
First@Rest@Extract[lis1, {{0}, {All, ;; , {1, 4, 5}}}]

both give all rows and columns 1, 4 and 5 in all matrices in lis1:

(* {{{a1, d1, e1}, {a2, d2, e2}}, {{a3, d3, e3}, {a4, d4, e4}}} *)
$\endgroup$
  • $\begingroup$ That only works for the specific example. OP said: "My problem is that the data I would like to select are not in an consecutive order (otherwise I could use the Part function)." $\endgroup$ – rm -rf May 2 '15 at 17:57
  • $\begingroup$ @TheToad, i may be wrong, but i thought the quote meant the OP was not aware of the part specifications other than from;;to (since Part specs can be any list). $\endgroup$ – kglr May 2 '15 at 18:08
  • $\begingroup$ @TheToad Even though the origins of the name change are clear, it will take (me) some time to get used to. Is it planned as permanent? $\endgroup$ – Leonid Shifrin May 2 '15 at 19:34
  • 1
    $\begingroup$ @LeonidShifrin It was just for fun, mostly. I came across an image that I really liked and just went along with it :) How long I'll keep it, I don't know, lol. I've always treated online personas as ephemeral and have fun changing things around but I can see how it might be confusing for others, especially when there are some artifacts and other bread crumbs lying around :) I can revert if you'd like me to/find it confusing. $\endgroup$ – rm -rf May 4 '15 at 4:34
  • $\begingroup$ @TheToad Oh no, by all means keep it as long as you want :). It's fun to everyone. I wasn't really serious. $\endgroup$ – Leonid Shifrin May 4 '15 at 9:00
5
$\begingroup$
lis1 /. b1|d1|b2|d2|b3|d3|b4|d4->Sequence[]
$\endgroup$

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.