2
$\begingroup$

Gday guys. This problem troubles me, and I hope you can help me solve it.

Say I have two random lists

L1={{2},{5},{1},{0},{4},{7},{8}}
L2={2,5,1,0,4,7,8}

and also a list

L3={3,5}

the list L3 means I don't want the elements in L1 at row 3 and 5, and elements in L2 at column 3 and 5. So I would like the results to come up like

resultsL1={{2},{5},{0},{7},{8}}
resultsL2={2,5,0,7,8}

I know how to use Delete or Drop to get rid of the row/column one by one, but I want an automatic way to do this job because the elements in L3 can have different elements and become any number in my original code. Thanks in advance!!

$\endgroup$

marked as duplicate by Mr.Wizard Feb 5 '15 at 10:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$

There are several ways but the one I like is the following:

If L1 and L2 have same length then:

index = Complement[Range[Length[L1]], L3];
L1[[index]]
(*{{2}, {5}, {0}, {7}, {8}}*)
L2[[index]]
(*{2, 5, 0, 7, 8}*)
$\endgroup$
  • $\begingroup$ Thank you Algohi. It works perfectly! $\endgroup$ – user11726 Feb 5 '15 at 3:40
  • $\begingroup$ My pleasure :). $\endgroup$ – Algohi Feb 5 '15 at 3:47
2
$\begingroup$

Try this:

    Delete[L1, {#} & /@ L3]
Delete[L2, {#} & /@ L3]

(*  {{2}, {5}, {0}, {7}, {8}}  *)

(*   {2, 5, 0, 7, 8}          *)

Have fun!

$\endgroup$
  • $\begingroup$ Nice! Thanks Alexi! $\endgroup$ – user11726 Feb 6 '15 at 6:47

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