Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7606 > unrolled thread

Sorting point-arrays by rows and columns, cont.

Started byChris Young <cy56@comcast.net>
First post2012-01-06 09:15 +0000
Last post2012-01-08 09:32 +0000
Articles 4 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Sorting point-arrays by rows and columns, cont. Chris Young <cy56@comcast.net> - 2012-01-06 09:15 +0000
    Re: Sorting point-arrays by rows and columns, cont. Chris Young <cy56@comcast.net> - 2012-01-07 10:27 +0000
      Re: Sorting point-arrays by rows and columns, cont. Chris Young <cy56@comcast.net> - 2012-01-08 09:27 +0000
      Re: Sorting point-arrays by rows and columns, cont. Chris Young <cy56@comcast.net> - 2012-01-08 09:32 +0000

#7606 — Sorting point-arrays by rows and columns, cont.

FromChris Young <cy56@comcast.net>
Date2012-01-06 09:15 +0000
SubjectSorting point-arrays by rows and columns, cont.
Message-ID<je6e3j$q6k$1@smc.vnet.net>
I don't see why what I'm doing below doesn't sort into rows and columns.



>From the Help on SortBy:
\!\(\*
ButtonBox["SortBy",
BaseStyle->"Link",
ButtonData->"paclet:ref/SortBy"]\)[e, f] is equivalent to \!\(\*
ButtonBox["Sort",
BaseStyle->"Link",
ButtonData->"paclet:ref/Sort"]\)[{f[#], #} & /@ e][[\!\(\*
ButtonBox["All",
BaseStyle->"Link",
ButtonData->"paclet:ref/All"]\), -1]]:
In[1098]:= e = RandomInteger[9, {10, 2}]
Out[1098]= {{3, 6}, {4, 6}, {6, 0}, {8, 7}, {1, 3}, {7, 4}, {8, 6}, {0, 
3}, {3, 0}, {4, 5}}
In[1099]:= SortBy[e, Last]
Out[1099]= {{3, 0}, {6, 0}, {0, 3}, {1, 3}, {7, 4}, {4, 5}, {3, 6}, {4, 
6}, {8, 6}, {8, 7}}
In[1101]:= {Last[#], #} & /@ e
Out[1101]= {{6, {3, 6}}, {6, {4, 6}}, {0, {6, 0}}, {7, {8, 7}}, {3, {1, 
3}}, {4, {7, 4}}, {6, {8, 6}}, {3, {0, 3}}, {0, {3, 0}}, {5, {4, 5}}}
In[1105]:= Sort[{Last[#], #} & /@ e]
Out[1105]= {{0, {3, 0}}, {0, {6, 0}}, {3, {0, 3}}, {3, {1, 3}}, {4, {7, 
4}}, {5, {4, 5}}, {6, {3, 6}}, {6, {4, 6}}, {6, {8, 6}}, {7, {8, 7}}}
In[1104]:= Sort[{Last[#], #} & /@ e]  [[1, -1]]
Out[1104]= {3, 0}
Sort[{Last[#], #} & /@ e]  [[All, -1]]
Out[1100]= {{3, 0}, {6, 0}, {0, 3}, {1, 3}, {7, 4}, {4, 5}, {3, 6}, {4, 
6}, {8, 6}, {8, 7}}
Trying to apply this to hexagonal point array:
In[1108]:= hex
Out[1108]= {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}, {-(1/2), Sqrt[3]/2}, {-1, 
0}, {-(1/2), -(Sqrt[3]/2)}, {1/2, -(Sqrt[3]/2)}}
In[1109]:= {Part[#, 2], Part[#, 1], #} & /@ hex
Out[1109]= {{0, 0, {0, 0}}, {0, 1, {1, 0}}, {Sqrt[3]/2, 1/2, {1/2, 
Sqrt[3]/2}}, {Sqrt[3]/2, -(1/2), {-(1/2), Sqrt[3]/2}}, {0, -1, {-1, 
0}}, {-(Sqrt[3]/2), -(1/2), {-(1/2), -(Sqrt[3]/2)}}, {-(Sqrt[3]/2), 
1/2, {1/2, -(Sqrt[3]/2)}}}
In[1110]:= Sort[%1109]
Out[1110]= {{0, -1, {-1, 0}}, {0, 0, {0, 0}}, {0, 1, {1, 0}}, 
{-(Sqrt[3]/2), -(1/2), {-(1/2), -(Sqrt[3]/2)}}, {-(Sqrt[3]/2), 1/2, 
{1/2, -(Sqrt[3]/2)}}, {Sqrt[3]/2, -(1/2), {-(1/2), Sqrt[3]/2}}, 
{Sqrt[3]/2, 1/2, {1/2, Sqrt[3]/2}}}

[toc] | [next] | [standalone]


#7636

FromChris Young <cy56@comcast.net>
Date2012-01-07 10:27 +0000
Message-ID<je96m6$j6c$1@smc.vnet.net>
In reply to#7606
Thanks to Fred Simons for this method. Seems to work fine and seems to
be one of the simplest to use in this particular case. Still wish that
Sort would just accept a list of successive conditions to break ties,
as SortBy does. (SortBy, however, applies the functions to every
element and _then_ sorts, not what we want here.)

In[3]:= Sort[hexPts, #1[[2]] < #2[[
     2]] \[Or] (#1[[2]] == #2[[2]] && #1[[1]] <= #2[[1]]) & ]

Out[3]= {{-(1/2), -(Sqrt[3]/2)}, {1/2, -(Sqrt[3]/2)}, {-1, 0}, {0,
  0}, {1, 0}, {-(1/2), Sqrt[3]/2}, {1/2, Sqrt[3]/2}}

To DrMajorBob:

Thanks for the example with Ordering, but I'm not sure why it would be
necessary here, if Sort works just as well:

Here's an ordering for irrationals on the second element:
In[4]:= pts = {{0, 2}, {1, Sqrt[2]}, {2, 2}, {4, 1}, {Sqrt[5], 1}, {6,
    1}, {Sqrt[3], 1}, {7, 0}, {-1, 0}};

In[5]:= pts[[Ordering[pts[[All, 2]], All, Less]]]

Out[5]= {{-1, 0}, {7, 0}, {Sqrt[3], 1}, {6, 1}, {Sqrt[5], 1}, {4, 1},
{1, Sqrt[2]}, {2, 2}, {0, 2}}

In[21]:= pts[[Ordering[pts[[All, 2]], All, #1 < #2 &]]]

Out[21]= {{-1, 0}, {7, 0}, {Sqrt[3], 1}, {6, 1}, {Sqrt[5], 1}, {4, 1},
{1, Sqrt[2]}, {2, 2}, {0, 2}}

It seems that we can do the same quicker with Sort:

In[25]:= Sort[pts, #1[[2]] < #2[[2]] &]

Out[25]= {{-1, 0}, {7, 0}, {Sqrt[3], 1}, {6, 1}, {Sqrt[5], 1}, {4, 1},
{1, Sqrt[2]}, {2, 2}, {0, 2}}


Thanks for the examples with Fold, which I'm going to continue to work
through, but I wasn't able to get it to sort the 2nd elements correctly:

In[10]:= threes = RandomChoice[Flatten[pts], {10, 3}]

Out[10]= {{1, 2, 0}, {1, Sqrt[2], 1}, {1, 7, 2}, {1, 1, Sqrt[5]}, {1,
2,  2}, {0, 4, -1}, {0, 2, 1}, {7, 1, 0}, {1, Sqrt[5], 2}, {0, 2, -1}}


(The following doesn't seem to work!! We have Sqrt[5] in the 2nd
coordinate coming before 1 in the last two vectors.
Don't understand why Reverse was needed, and also why {3,1} was used.
What does the {3,1} to?)

In[35]:= Fold[#1[[Ordering[#1[[All, #2]], All, LessEqual]]] &, threes,
 Reverse@{3, 1}]

Out[35]= {{0, 4, -1}, {0, 2, -1}, {1, 2, 0}, {7, 1, 0}, {0, 2, 1}, {1,
Sqrt[2], 1}, {1, 7, 2}, {1, 2, 2}, {1, Sqrt[5], 2}, {1, 1, Sqrt[5]}}


In[29]:= N[%26, 2]

Out[29]= {{0, 4.0, -1.0}, {0, 2.0, -1.0}, {1.0, 2.0, 0}, {7.0, 1.0, 0},
{0, 2.0, 1.0}, {1.0, 1.4, 1.0}, {1.0, 7.0, 2.0}, {1.0, 2.0, 2.0}, {1.0,
 2.2, 2.0}, {1.0, 1.0, 2.2}}

[toc] | [prev] | [next] | [standalone]


#7661

FromChris Young <cy56@comcast.net>
Date2012-01-08 09:27 +0000
Message-ID<jebnhs$1bu$1@smc.vnet.net>
In reply to#7636
Despite what I said, it seems to be working now. Fold[ ] looks useful 
for more complicated sorting especially.

In[75]:= FoldList[
 #1[[Ordering[#1[[All, #2]], All, LessEqual]]] &,
 hexPts,
 {1, 2}
 ]
The first list below is hexPts unaltered.
The second is hexPts sorted by the x coordinates, i.e., by columns.
The third is hexPts sorted by the y coordinates, but with the 
x-coordinates ordered within each row.

Out[75]=
{
{{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}, {-(1/2), Sqrt[3]/2}, {-1, 0}, 
{-(1/2), -(Sqrt[3]/2)}, {1/2, -(Sqrt[3]/2)}},
 {{-1, 0}, {-(1/2), Sqrt[3]/2}, {-(1/2), -(Sqrt[3]/2)}, {0, 0}, {1/2,  
Sqrt[3]/2}, {1/2, -(Sqrt[3]/2)}, {1, 0}},
{{-(1/2), -(Sqrt[3]/2)}, {1/2, -(Sqrt[3]/2)}, {-1, 0}, {0, 0}, {1, 0}, 
{-(1/2), Sqrt[3]/2}, {1/2, Sqrt[3]/2}}
}

On 2012-01-07 10:27:18 +0000, Chris Young said:

> Thanks for the examples with Fold, which I'm going to continue to work
> through, but I wasn't able to get it to sort the 2nd elements correctly:
> 
> In[10]:= threes = RandomChoice[Flatten[pts], {10, 3}]
> 
> Out[10]= {{1, 2, 0}, {1, Sqrt[2], 1}, {1, 7, 2}, {1, 1, Sqrt[5]}, {1,
> 2,  2}, {0, 4, -1}, {0, 2, 1}, {7, 1, 0}, {1, Sqrt[5], 2}, {0, 2, -1}}
> 
> 
> (The following doesn't seem to work!! We have Sqrt[5] in the 2nd
> coordinate coming before 1 in the last two vectors.
> Don't understand why Reverse was needed, and also why {3,1} was used.
> What does the {3,1} to?)
> 
> In[35]:= Fold[#1[[Ordering[#1[[All, #2]], All, LessEqual]]] &, threes,
>  Reverse@{3, 1}]
> 
> Out[35]= {{0, 4, -1}, {0, 2, -1}, {1, 2, 0}, {7, 1, 0}, {0, 2, 1}, {1,
> Sqrt[2], 1}, {1, 7, 2}, {1, 2, 2}, {1, Sqrt[5], 2}, {1, 1, Sqrt[5]}}


[toc] | [prev] | [next] | [standalone]


#7668

FromChris Young <cy56@comcast.net>
Date2012-01-08 09:32 +0000
Message-ID<jebnrf$1i9$1@smc.vnet.net>
In reply to#7636
It's a little hard to get used to the conventions about which slot gets 
which argument in a lot of these functions. For example, in the Help 
for FoldList, it just has an example, which doesn't even use the usual 
sort order.

Fold to the right:
In[1]:= FoldList[g[#2, #1] &, x, {a, b, c, d}]
Out[1]= {x, g[a, x], g[b, g[a, x]], g[c, g[b, g[a, x]]],
 g[d, g[c, g[b, g[a, x]]]]}

I wish the documentation would at least say what the slots are for. One 
self-documenting way to write the code is with the tee-arrow notation 
for funcitons (via Esc-fn-Esc). Then we can just put in "P" and "list" 
for the arguments and everything is a lot clearer.

In[78]:= P = hexPts;
list = {1, 2};

FoldList[
 {P, list} \[Function]
  P[[Ordering[P[[All, list]], All, LessEqual]]],
 P,
 list
 ]

Out[80]=
 {
{{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}, {-(1/2), Sqrt[3]/2}, {-1, 0}, 
{-(1/2), -(Sqrt[3]/2)}, {1/2, -(Sqrt[3]/2)}},
{{-1,  0}, {-(1/2), Sqrt[3]/2}, {-(1/2), -(Sqrt[3]/2)}, {0, 0}, {1/2, 
Sqrt[3]/2}, {1/2, -(Sqrt[3]/2)}, {1, 0}},
{{-(1/2), -(Sqrt[3]/2)}, {1/2, -(Sqrt[3]/2)}, {-1, 0}, {0, 0}, {1, 0}, 
{-(1/2), Sqrt[3]/2}, {1/2, Sqrt[3]/2}}
}

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web