MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: sort and positon matrix element help

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73073] Re: [mg73050] sort and positon matrix element help
  • From: János <janos.lobb at yale.edu>
  • Date: Thu, 1 Feb 2007 03:23:02 -0500 (EST)
  • References: <200701310512.AAA15216@smc.vnet.net>

On Jan 31, 2007, at 12:12 AM, comp.soft-sys.math.mathematica wrote:

> Hi, guys,
>
>         I am attacking one proble and may need your help.
>         I have a large matrix(m*n). I wanna to sort out the matrix
> from large to small according to its element's value AND save the
> corresponding row and column number as its position. Latter on, plot
> out the first 20 largest value accroding to their positions.
>         Is there any good way to do this, especially take out the
> positons of element and assocate it with the value?

Here is a newbie approach:

In[1]:=
n = 5;
m = 7;

In[3]:=
mat = Table[Table[Random[
      Integer, {10, 99}],
     {j, m}], {i, n}]
Out[3]=
{{16, 80, 60, 16, 29, 79,
    60}, {63, 13, 73, 15, 56,
    26, 86}, {16, 10, 68, 41,
    27, 98, 77}, {13, 55, 91,
    87, 27, 22, 21},
   {54, 25, 17, 55, 69, 69,
    85}}

In[4]:=
umemb = Reverse[Union[
     Flatten[mat]]]
Out[4]=
{98, 91, 87, 86, 85, 80, 79,
   77, 73, 69, 68, 63, 60, 56,
   55, 54, 41, 29, 27, 26, 25,
   22, 21, 17, 16, 15, 13, 10}

In[5]:=
({#1, Position[mat,
      #1]} & ) /@ umemb
Out[5]=
{{98, {{3, 6}}},
   {91, {{4, 3}}},
   {87, {{4, 4}}},
   {86, {{2, 7}}},
   {85, {{5, 7}}},
   {80, {{1, 2}}},
   {79, {{1, 6}}},
   {77, {{3, 7}}},
   {73, {{2, 3}}},
   {69, {{5, 5}, {5, 6}}},
   {68, {{3, 3}}},
   {63, {{2, 1}}},
   {60, {{1, 3}, {1, 7}}},
   {56, {{2, 5}}},
   {55, {{4, 2}, {5, 4}}},
   {54, {{5, 1}}},
   {41, {{3, 4}}},
   {29, {{1, 5}}},
   {27, {{3, 5}, {4, 5}}},
   {26, {{2, 6}}},
   {25, {{5, 2}}},
   {22, {{4, 6}}},
   {21, {{4, 7}}},
   {17, {{5, 3}}},
   {16, {{1, 1}, {1, 4},
     {3, 1}}}, {15, {{2, 4}}},
   {13, {{2, 2}, {4, 1}}},
   {10, {{3, 2}}}}

With the best,

János


----------------------------------------------
Trying to argue with a politician is like lifting up the head of a  
corpse.
(S. Lem: His Master Voice)


  • Prev by Date: Limit question
  • Next by Date: Re: & without #
  • Previous by thread: Re: sort and positon matrix element help
  • Next by thread: Re: sort and positon matrix element help