4
$\begingroup$

We have data in a list called megadata where megadata[[1]] for instance are the temperatures of each data point, megadata[[2]] are the pressures, etc. We are trying to selectively remove data points to see how it changes our fits but I have been unable to get Delete or Drop or anything else to work. I need something to let me remove the same position from every nested list within the list. So for instance if I wanted to delete positions 2 and 3 I need something to change megadata = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} into `megadata = {{1, 4}, {5, 8}, {9, 12}.

Edit: I'm really new to Mathematica (learning ahead of the semester as part of undergraduate research) so I'm having trouble understanding some of the comments made. I used the Drop[#, {2, 3}] & /@ megadata idea and it worked quite nicely for removing adjacent points but my nested lists have a length of 12 and when I would try using it as Drop[#, {4, 7}] & /@ megadata, it seemed to remove all the points between 4 and 7 (so it would remove 4,5,6, and 7). Is there anyway to modify this format slightly to make it not remove that whole range of points and only remove 4 and 7?

Edit 2: I actually think I found the answer. Didn't see the link to the duplicate answer above. Sorry real new to all of this. Thanks for all the help though!

$\endgroup$
6
  • 1
    $\begingroup$ megadata[[All, {1, 4}]] $\endgroup$ Commented Jul 15, 2014 at 22:59
  • $\begingroup$ This is closely related to Manipulating a sublist based on position although there are even closer posts, you should find them. $\endgroup$
    – Artes
    Commented Jul 15, 2014 at 23:00
  • $\begingroup$ If you want to remove points i order to test a fit then I'd suggest using Cases with some sort of condition applied to a pattern. But we'd need an example of your real data. $\endgroup$ Commented Jul 15, 2014 at 23:03
  • $\begingroup$ @artes Your +1 and accepted answer in the cited link is somehow but not "closely" related :) $\endgroup$
    – eldo
    Commented Jul 15, 2014 at 23:21
  • $\begingroup$ @eldo Thanks for an upvote, however a remark is needed, there is some kind of relativity in the concept of distance, so I don't insist that it is universal. When it comes to this problem I have to ask why you don't use a simpler and nicer approach: list /. {a_, b_, c_, d_} :> {a, d}? $\endgroup$
    – Artes
    Commented Jul 15, 2014 at 23:37

2 Answers 2

2
$\begingroup$

Both Delete and Drop should certainly work if used with Map.

data = Partition[Range[4 4], 4]
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}
Drop[#, {2, 3}] & /@ data
Delete[#, {{2}, {3}}] & /@ data

Both return

{{1, 4}, {5, 8}, {9, 12}, {13, 16}}

But as eldo points out, Map can be avoided.

data[[All, {1, 4}]]

also works. Notice in this last case, you specify the columns you want to keep, rather than the ones you want to drop.

$\endgroup$
1
$\begingroup$
data={{1,2,3,4},{5,6,7,8},{9,10,11,12}}

Part[#, {1, -1}] & /@ data

{{1, 4}, {5, 8}, {9, 12}}

Cases[data, {a_, __, b_} :> {a, b}]

and many more possibilities

$\endgroup$
3
  • $\begingroup$ you don't need to use Map. see my comment above $\endgroup$ Commented Jul 15, 2014 at 23:04
  • $\begingroup$ @Mike Thanks for the tip :) $\endgroup$
    – eldo
    Commented Jul 15, 2014 at 23:14
  • $\begingroup$ data[[All,{1,-1}]] but my other comment was more like DeleteCases[data, {a_/;some condition, __, b_/;maybe condition here too}] etc because he wants to remove points from a fit $\endgroup$ Commented Jul 15, 2014 at 23:18

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