2
$\begingroup$

I have a Grid in which I want to modify the background of the minimum value of the last row. For that purpose, I have create a variable with the position of the minimum value of this row "PosicionECMMin = Position[ECMEst, Min[ECMEst]]" but that does not work. Can anyone help me. I attached an image where you can see what I want. Thank you very much.

Set the background minimum value of the last row

{EspEst, VarEst, ECMEst} = RandomReal[1, {3, 4}]
PosicionECMMin = Position[ECMEst, Min[ECMEst]]


Grid[{{"", 
"T1", 
"T2", 
"T3", 
"T4"}, 
Prepend[EspEst, "Esperanza"], Prepend[VarEst, "Varianza"], 
Prepend[ECMEst, "ECM"]}, Frame -> All, Alignment -> Center, 
Spacings -> 2, 
Background -> {None, None, {4, PosicionECMMin + 1} -> Green}]
$\endgroup$
  • $\begingroup$ ECMEst has numeric values as you can see in the image that I have attached. To see the value of the "PosicionECMMin" I have printed his value and I have seen that It is correct but when I put this variable into the Background code, it does not work. Thanks. $\endgroup$ – Pcouper Oct 29 '17 at 20:11
5
$\begingroup$

You got the position specification wrong. PosicionECMMin + 1 in your answer is not the scalar it is supposed to be.

As M. Stern points out, the most straightforward way to fix this is to replace

PosicionECMMin = Position[ECMEst, Min[ECMEst]]

in your code, with

PosicionECMMin = Position[ECMEst, Min[ECMEst]][[1, 1]]

This also works:

{EspEst, VarEst, ECMEst} = RandomReal[1, {3, 4}];
PosicionECMMin = 
  Prepend[FirstPosition[ECMEst, Min[ECMEst]], 4] + {0, 1};

Grid[{
  {"", "T1", "T2", "T3", "T4"},
  Prepend[EspEst, "Esperanza"],
  Prepend[VarEst, "Varianza"],
  Prepend[ECMEst, "ECM"]
  },
 Frame -> All,
 Alignment -> Center,
 Spacings -> 2,
 Background -> {None, None, PosicionECMMin -> Green}
 ]

Mathematica graphics

Another approach would be to not use the Background property, but to use Item instead:

PosicionECMMin = FirstPosition[ECMEst, Min[ECMEst]];
ECMEst = ReplacePart[ECMEst, 
  PosicionECMMin -> 
   Item[Extract[ECMEst, PosicionECMMin], Background -> Green]]

Grid[{
  {"", "T1", "T2", "T3", "T4"},
  Prepend[EspEst, "Esperanza"],
  Prepend[VarEst, "Varianza"],
  Prepend[ECMEst, "ECM"]
  },
 Frame -> All,
 Alignment -> Center,
 Spacings -> 2
 ]

The Item approach can also be used with TableForm:

{EspEst, VarEst, ECMEst} = RandomReal[1, {3, 4}];
PosicionECMMin = FirstPosition[ECMEst, Min[ECMEst]];
ECMEst = ReplacePart[
   ECMEst,
   PosicionECMMin -> Item[Extract[ECMEst, PosicionECMMin], Background -> Green]
   ];

TableForm[
 {EspEst, VarEst, ECMEst},
 TableHeadings -> {{"Esperanza", "Varianza", "ECM"}, {"T1", "T2", "T3", "T4"}}
 ]

Mathematica graphics

Beware that the Item approach modifies ECMEst. If you intend to keep doing other work with ECMEst then you'll want to copy the values to another variable first.

$\endgroup$
  • 1
    $\begingroup$ or simply add [[1,1]] after Position[ECMEst, Min[ECMEst]] $\endgroup$ – M. Stern Oct 29 '17 at 23:45
  • $\begingroup$ @M.Stern In OP's original version, yes. Not in my versions. $\endgroup$ – C. E. Oct 30 '17 at 2:17
  • $\begingroup$ Yes. Now that I think about it.. using FirstPosition is better in general $\endgroup$ – M. Stern Oct 31 '17 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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