Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.6K Views
|
7 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Sort properly random real number triples between -1,1?

Posted 9 years ago

Why isn't this list sorting properly?

Here I generate a list of 100 random real number triples between -1,1

 Clear[randomTriples];
 randomTriples = Table[{RandomReal[{-1, 1}, 3]}, 100];
 randomTriples

I want to sort this in increasing order, such as:

 Sort[randomTriples, #1 > #2 &]

But this isn't sorting the way I expected it. I used the documentation of "Sort" for help.

Once I can get the code above to sort correctly, I would also like to sort based on the magnitude of the second element of the triples, such as:

 Sort[randomTriples, #1[[2]] < #2[[2]] &]

Why aren't either of these methods working? Like I said, I used the documentation for Sort but I cant get the output I would like..I feel like what I have is a valid approach but obviously not.

POSTED BY: Brandon Davis
7 Replies
Anonymous User
Anonymous User
Posted 9 years ago

(excuse reposts i had no idea i was waiting for approved or for wait times between posts)

Mathematica Sort can walk any Lists and is efficient for doing it. But it's not "configurable"? It's easier to arrange your input, as in the below.

Like allot of things in Mathematica: it isn't there because you don't need it and you don't want it :) Your talking about people like Donald Knuth had hands in making things in Mathematica - people who keep things trim not full of junk because you do need it that way even if you know it yet :) It's not needed because everything you need is already handy and ready and better as a general tool without allot of complicated function arguments (program the Mathematica way, by lists). (obviously, some things are just missing - but carfully think if it isn't missing because you don't want it, trust me)

Sort@Table[{RandomReal[{-1, 1}, 3]}, 10] 

works correctly, walks dimensions as it should, though you may prefer a different order.

in {a,b,c}, it orders all a's first, then all b's, then all c's. it chooses to sort numerically since that is the data type.

you don't have to study sort options. give it {b,a,c} if you want that, no need to give it {a,b,c} and juggle options to make it treat it differently :)

Table[{{i, j, k}}, {i, -1, 1}, {j, -1, 1}, {k, -1, 1}] // Sort

Table[{{j, i, k}}, {i, -1, 1}, {j, -1, 1}, {k, -1, 1}] // 
 Sort /. {b_, a_, c_} -> {a, b, c}

or instead of /.

{%[[All, 1]], %[[All, 2]], %[[All, 3]]}

Mathematica will be very efficient shuffle lists order by using symbols (like the above), so there is no reason to avoid re-ordering lists, it's not like making a new program copy, it just moves symbols representing a big list around.

OrderedQ[{#1,#2}]& lets you change the way things are ordered (ie, you program it to order depending on your data and needs)

Read some .m code for Mathematica packages, some from earlier editions perhaps (they are shorter). You'll see programming the Mathematica way is more powerful than the functional programming you learned in (C?) and there is a section in The Mathematica Book describing why.

POSTED BY: Anonymous User
Posted 9 years ago

Is this what you mean?

SortBy[randomTriples, #[[All, 2]] &]

Replace the No 2 by 1, 2, or 3 to sort by that column, this sorts small to large, to sort large to small use

SortBy[randomTriples, -#[[All, 2]] &]
POSTED BY: Paul Cleary
Posted 9 years ago

This actually produces an error.. Does it not for you? It says "depth of object {} is not sufficient for the given part specifications. And by {} I mean all the triples..

POSTED BY: Brandon Davis
Posted 9 years ago

This sorts the list by column 2 with no errors on my end

Clear[randomTriples]; randomTriples = 
 Table[{RandomReal[{-1, 1}, 3]}, 100];
SortBy[randomTriples, -#[[All, 2]] &]
POSTED BY: Paul Cleary
Posted 9 years ago

Paul your right, it was an error on my part... but doesn't sorting on the basis of magnitude mean absolute value??

POSTED BY: Brandon Davis
Posted 9 years ago
randomTriples = Table[RandomReal[{-1, 1}, 3], 100] (*remove curly brackets if you don't need them*)
Sort[#, Greater] & /@ randomTriples
POSTED BY: Girish Arabale

Indeed, your randomtriples has an extra set of braces. so [[2]] does not exist. If you want to sort 'something' by magnitude, always use SortBy as it is much faster, and the notation is more legible. Say you want to sort triplets by their Total:

random = RandomReal[{0, 1}, {100000, 3}];
time1 = AbsoluteTiming[out1 = Sort[random, Total[#1] < Total[#2] &];]
time2 = AbsoluteTiming[out2 = SortBy[random, Total];]
out1 == out2
time1[[1]]/time2[[1]]

same output, 420x faster (on my laptop), and easier to read.

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