Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Consider a big list where all the inner lists length's are the same

 list = {{x1, y1, z1, t1}, {x2, y2, z2, t2}, {x3, y3, z3, t3}, ...};
 elementNo = {4, 3, 2};

I want output like this,

{{x1}, {x2}, {x3}, ...}

That is, when I specify elementNo as {4, 3, 2}, I mean the elements at positions 2, 3 and 4 in the sub-lists should be removed -- I want the elements at position 1 only. When I specify elementNo as {2, 3}, I want

 {{x1, t1}, {x2, t2}, {x3, t3}, ...}

How can I do this?

share
    
Hm... okay, I guess I see now. You should describe this problem as deleting elements, as that is what it is. –  Mr.Wizard Mar 18 '13 at 6:07
    
@Mr.Wizard I tried for getting that output,but I didn't get it. –  subbu Mar 18 '13 at 6:10
    
@Mr.Wizard we have to remove the specific position elements from the main list,that specific element position getting from elementNO. –  subbu Mar 18 '13 at 6:12
    
I answered this. Please take the time to Accept some of the answers to your earlier questions. –  Mr.Wizard Mar 18 '13 at 6:15
    
@m_goldberg You're obsessive about your edits, aren't you? –  Mr.Wizard Mar 18 '13 at 6:52

7 Answers 7

Another option:

f[list_, pos_] := Module[{x = list},
  x[[All, pos]] = Sequence[];
  x]
share
    
+1 Short, sweet, and fast. –  m_goldberg Mar 18 '13 at 13:07
f1[list_, spec_] := With[{sp = List /@ spec}, Delete[#, sp] & /@ list]

f1[list, {4, 3, 2}]
f1[list, {2, 3}]
{{x1}, {x2}, {x3}}
{{x1, t1}, {x2, t2}, {x3, t3}}
f2[list_, spec_] := list[[ All, Complement[Range@Length@First@list, spec] ]]

f2[list, {4, 3, 2}]
f2[list, {2, 3}]
{{x1}, {x2}, {x3}}
{{x1, t1}, {x2, t2}, {x3, t3}}
share
3  
+1. Note that a more efficient form of List /@ specis Transpose[{spec}]. –  Leonid Shifrin Mar 18 '13 at 8:33
    
@Leonid True, however I thought clarity might be better for this answer, and f2 is the higher performance method anyway. –  Mr.Wizard Mar 18 '13 at 10:37
    
Ok, makes sense. –  Leonid Shifrin Mar 18 '13 at 11:11

Here's another alternative:

f[list_, pos_] := With[{p = pos ~Sort~ Greater}, Fold[# ~Drop~ {#2} &, #, p] & /@ list]

f[list, {2, 3}]
(* {{x1, t1}, {x2, t2}, {x3, t3}} *)

If you can guarantee that the positions to be dropped will be input in descending order, you can drop the sorting step.

share

Try this one

list = RandomInteger[50, {10, 4}]
new = {};
elemNo = {4, 3, 2};
new = Map[Delete[#, {{elemNo[[1]]}, {elemNo[[2]]}, {elemNo[[3]]}}] &,list]
share
1  
That won't work if the elemNo list is a different length. It's also not efficiently written, as you could much more easily do: Map[Delete[#, List /@ elemNo] &, list]. This is what I did in my answer for the function f1 but with the additional optimization of doing the List /@ elemNo once and inserting it using With. –  Mr.Wizard Mar 18 '13 at 6:49

here is the way I would do it using my Excel mindset,

list = {{x1, y1, z1, t1}, {x2, y2, z2, t2}, {x3, y3, z3, t3}};
elementNo = {4, 3, 2};
todelete = {#} & /@ Sort@elementNo
tokeep = Delete[Range@Length@First@list, todelete]
remaining = Take[list, All, tokeep]

or something similar.

share

Another way, using Outer and Extract:

data = {{x1, y1, z1, t1}, {x2, y2, z2, t2}, {x3, y3, z3, t3}};
unwanted1 = {4, 3, 2};
unwanted2 = {2, 3};

extractComplement[data_List, spec_List] :=
  Module[{dataSize, subSize, survivors},
    dataSize = Length@data;
    subSize = Length@data[[1]];
    survivors = Outer[List, Range@dataSize, Complement[Range@subSize, spec]];
    Extract[data, #] & /@ survivors]

extractComplement[data, unwanted1]

{{x1}, {x2}, {x3}}

extractComplement[data, unwanted2]

{x1, t1}, {x2, t2}, {x3, t3}

share

If all sublists are indeed of the same length, use Transpose:

Transpose@Delete[Transpose@list, List /@ elementNo]
{{x1}, {x2}, {x3}}

Note that you can use the shorthand notation for Transpose, which makes the code really short:

Mathematica graphics

With other elementNo:

elementNo = {3, 2};
Transpose@Delete[Transpose@list, List /@ elementNo]
{{x1, t1}, {x2, t2}, {x3, t3}}
share
    
While you're using Transpose how about Delete[list\[Transpose], {elementNo}\[Transpose]]\[Transpose]? Looks great in the Notebook. :-) –  Mr.Wizard Mar 18 '13 at 10:35
    
@Mr.Wizard I've set my PerformanceGoal to "Speed" instead of "CodeGolf". An extra Transpose might be just too much for a modern computer... :) –  István Zachar Mar 18 '13 at 10:41

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .