Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.6K Views
|
1 Reply
|
4 Total Likes
View groups...
Share
Share this post:

Find maximum values from a list of pairs using MaximalBy?

Posted 9 years ago

Howdy there! I'm having some trouble using the MaximalBy function in Mathematica. I'm feeding the function a list of ordered pairs, indexing groups of these pairs within the list, and then printing the maximum values. My hope would be that the print statement would simply be a bunch of unique ordered pairs(every pair in the original list is unique). However, I'm getting a list full of repeated entries and empty list. Any ideas as to what could be causing this? Here is my code:

Do[Print[MaximalBy[Table[values[[i]], {i, 100 j + 1, 100 z}], Last]], {j, 0, 1}, {z, 1, 2}]

And here is the result:

{{0.007375,0.00173}}
{{0.022375,0.00195}}
{}
{{0.022375,0.00195}}

And here is an idea of what I would like to see:

{{0.007375,0.00173}}
{{0.022375,0.00195}}

Thanks in advance!

POSTED BY: Alexei Smith

The first principle of debugging Mathematica (or even just writing new code): write and test small pieces first! Don't write a complex piece of code like what you are showing and then wonder why it doesn't do what you think it should be doing. Build it up pice by piece, testing each piece in turn. With a notebook interface it's easy to do that. It is in fact one of the major advantages of an interactive notebook interface.

So first thing you should do before using MaximalBy: try it out and see what it does! Look it up in the documentation and check the examples.

You'll see that MaximalBy always returns a list of results, regardless of how many results are there. There can be zero, one or multiple results.

In[13]:= values = {{1, 2}, {2, 2}, {3, 1}};

In[17]:= MaximalBy[{}, Last]
Out[17]= {}

In[18]:= MaximalBy[values, First]
Out[18]= {{3, 1}}

In[19]:= MaximalBy[values, Last]
Out[19]= {{1, 2}, {2, 2}}
POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard