4
$\begingroup$

If I’ve got a big array displayed in Mathematica, is there a way for me to select a particular location on the screen and have Mathematica tell me “You’ve selected the entry in row 137 and column 92” (or whatever)?

I don’t want to painstakingly count rows or columns to do this myself.

The best kludge I’ve found so far is to select the part of the row or column up to the entry in question, copy that, paste it elsewhere as a list, and then determine the length of that list. But this seems needlessly roundabout, and it’s annoying to do when I want to determine the location of not just one entry but several of them in succession.

Is there a better way?

(For that matter, is there a way to tell Mathematica “Display the matrix with the entry in row 137 and column 92 highlighted”?)

$\endgroup$

3 Answers 3

3
$\begingroup$

While I think of a way to accomplish your first request, let me contribute this helper function that can highlight positions in a table-formatted array for you, i.e. the very last part of your question):

Clear[highlight]
highlight[list_, position_] := 
  Grid[array, Background -> {None, None, # -> Yellow & /@ position}]

Let's create an array of random numbers and try it out:

yourarray = RandomInteger[{0, 10}, {30, 4}];
highlight[yourarray, {{3, 2}, {2, 3}}]

Mathematica graphics

Position specifications will be silently ignored if they are outside of your array (e.g. {4, 6} for the example above), or they do not have your array's dimensions (e.g. {4, 3, 4}).

$\endgroup$
8
$\begingroup$
data = RandomInteger[100, {15, 15}];

styleElement[x_] := MouseAppearance[
  Mouseover[x, Style[x, Red]],
  "LinkHand"
  ]

showIndex[x_, index_] := EventHandler[x, {"MouseUp" :> MessageDialog[index]}]

Outer[
   showIndex[styleElement@data[[##]], {##}] &,
   Range[15], Range[15]
   ] // Grid // Deploy

enter image description here

Or you can use Tooltip:

styleElement[x_, index_] := Tooltip[MouseAppearance[
   Mouseover[x, Style[x, Red]],
   "LinkHand"
   ], index]

Outer[
   styleElement[data[[##]], {##}] &,
   Range[15], Range[15]
   ] // Grid // Deploy
$\endgroup$
2
  • $\begingroup$ Neat! Outer is the secret sauce I was missing. I like the Tooltip version best myself, but they are both quite nice. (+1) $\endgroup$
    – MarcoB
    Commented May 26, 2015 at 19:29
  • $\begingroup$ @MarcoB We had nice teamwork on this question, answering one part of it each :) +1 to you too. $\endgroup$
    – C. E.
    Commented May 26, 2015 at 22:09
3
$\begingroup$

Here is an approach using DynamicWrapper. Display either as grid or matrix and highlighting style obviously customizable. Thank you kuba for introducing me to DynamicWrapper some time ago.

f = DynamicModule[{col = Black, bg = White}, 
    DynamicWrapper[Dynamic@Style[#1, col, Background -> bg], 
     If[CurrentValue["MouseOver"], col = Red; bg = Yellow; pos = #2, 
      col = Black; bg = White; pos = {}]]] &;
mt = Thread[{RandomInteger[{0, 10}, 100], Tuples[Range[10], 2]}];
Row[{Grid[Partition[f @@@ mt, 10], Frame -> All], "\[RightArrow]", 
  Dynamic@pos}]
Row[{MatrixForm[Partition[f @@@ mt, 10]], "\[RightArrow]", 
  Dynamic@pos}]

enter image description here

$\endgroup$

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.