Message Boards Message Boards

0
|
7970 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Eliminate elements from nested lists that meet a certain criteria?

Posted 7 years ago

Hello! I'm very new to Mathematica and I am trying to extract elements of a nested list based on whether their average value is above a certain threshold.
Given a nested list:

j = {{5,2,3},{2},{1,4,5},{1,1,2},{1,2,4}}

I'd like to only retain lists with average values above 3, and eliminate everything else (while keeping the structure of the list). I thought using something along the lines of:

f = DeleteCases[j, Map[Mean, j, {2}] < 3, {2}]

But it's not working out. Any help would be greatly appreciated. What I would be looking for is to get a final form of

f= {{5,2,3},{1,4,5}}
POSTED BY: Greg Morrin
5 Replies
Posted 7 years ago

It seems to a difficult methods. Anybody please suggest me a simple method to eliminate eliminate elements from nested lists.

POSTED BY: stennial mike
Posted 7 years ago
POSTED BY: Bill Simpson
Select[j, Mean@# > 3 &]

Cases[j, x_ /; Mean@x > 3]

DeleteCases[j, x_ /; Mean@x <= 3]
Posted 7 years ago

Here is one way, using a pure function to replace lists with mean less than 3 with Nothing.

In[1]:= j = {{5, 2, 3}, {2}, {1, 4, 5}, {1, 1, 2}, {1, 2, 4}};

In[2]:= If[Mean[#] > 3, #, Nothing] & /@ j

Out[2]= {{5, 2, 3}, {1, 4, 5}}
POSTED BY: David Keith
Posted 7 years ago

Thanks David! Didn't know there was a "Nothing" argument. Works well.

POSTED BY: Greg Morrin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract