0
$\begingroup$

I have the following array in both MatrixForm and List form

0.474042    0.507508    0.4977      0.539773

0.473447    0.50765     0.483809    0.52489

0.46797     0.483669    0.494823    0.516956

0.467079    0.483509    0.478704    0.501286


A = {{ 0.474042, 0.507508, 0.4977,   0.539773},
     { 0.473447, 0.50765,  0.483809, 0.52489 },
     { 0.46797,  0.483669, 0.494823, 0.516956},
     { 0.467079, 0.483509, 0.478704, 0.501286}
    }

I want to get the max value of each column. I've tried the following for the first columns but no help:

Max[A[[All, 1]]]

Any comment will be greatly appreciated.

$\endgroup$
7
  • 4
    $\begingroup$ Max/@Transpose@A ? $\endgroup$ Sep 19, 2013 at 19:31
  • 1
    $\begingroup$ Well actually using Max[] on each column does the trick. Your syntax is correct for the first column. $\endgroup$
    – Wizard
    Sep 19, 2013 at 19:32
  • $\begingroup$ I am getting weird result though. Here it's: Max[0.467079,0.46797,0.473447,0.474042] Any recommendation? $\endgroup$
    – UTK
    Sep 19, 2013 at 19:44
  • $\begingroup$ @UTK that seems very strange as that should evaluate to 0.474042 automatically. Does Definition[Max] have anything more than just Attributes[Max]=...? If it does you can restart the kernel to reset all definitions you have made. (In the menu Evaluation->Quit kernel->Local) $\endgroup$
    – ssch
    Sep 19, 2013 at 19:49
  • 2
    $\begingroup$ Is it possible you have "strings" in A? Does A[[1,1]]//FullForm have " around the value? $\endgroup$
    – ssch
    Sep 19, 2013 at 19:51

2 Answers 2

2
$\begingroup$

As already recommended by belisarius but once again as complete code to leave no stone unturned:

A = {{0.474042, 0.507508, 0.4977, 0.539773}, {0.473447, 0.50765, 0.483809, 
    0.52489}, {0.46797, 0.483669, 0.494823, 0.516956}, {0.467079, 0.483509, 0.478704, 
    0.501286}};

A // MatrixForm

$\left( \begin{array}{cccc} 0.474042 & 0.507508 & 0.4977 & 0.539773 \\ 0.473447 & 0.50765 & 0.483809 & 0.52489 \\ 0.46797 & 0.483669 & 0.494823 & 0.516956 \\ 0.467079 & 0.483509 & 0.478704 & 0.501286 \end{array} \right)$

Max /@ Transpose[A]
{0.474042, 0.50765, 0.4977, 0.539773}

Also less efficiently:

MapThread[Max, A]
{0.474042, 0.50765, 0.4977, 0.539773}
$\endgroup$
3
  • $\begingroup$ Thank you all for the comments.I have tried all of the methods that you mentioned here but for some reasons it is not working yet. I have a huge code and this matrix is in a for loop. Do you think reason might be the for loop ? I can share the part of my code if you think it helps. But it's so weird that it's not working in this code but works in other examples. $\endgroup$
    – UTK
    Sep 20, 2013 at 14:06
  • $\begingroup$ It is working now. Thank you all. $\endgroup$
    – UTK
    Sep 20, 2013 at 17:17
  • $\begingroup$ @UTK I'm glad you have a solution but I'm curious: what was the problem? Did you have a table of Strings as ssch suggested? $\endgroup$
    – Mr.Wizard
    Sep 21, 2013 at 0:31
1
$\begingroup$
A = { {0.474042,0.507508,0.4977,0.539773},  {0.473447,0.50765,0.483809,0.52489},
     {0.46797,0.483669,0.494823,0.516956}, {0.467079,0.483509,0.478704,0.501286}}

Try

Max @ A[[All, #]] & /@ Range[4]

Which gives:

{0.474042, 0.50765, 0.4977, 0.539773}

You can also do:

Max @@@ (A\[Transpose])
$\endgroup$
3
  • $\begingroup$ Well, for some reasons I am getting the following: {Max[0.467079,0.46797,0.473447,0.474042], Max[0.483509,0.483669,0.507508,0.50765], Max[0.478704,0.483809,0.494823,0.4977], Max[0.501286,0.516956,0.52489,0.539773]} Any idea? $\endgroup$
    – UTK
    Sep 19, 2013 at 19:41
  • $\begingroup$ Did you include the ampersand? I can't reproduce your result. Maybe start a new Mathematica session. $\endgroup$
    – RunnyKine
    Sep 19, 2013 at 19:48
  • $\begingroup$ Obviously Max does not evaluate. Restart the kernel as mentioned above. $\endgroup$
    – Wizard
    Sep 19, 2013 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.