**Update:** If the same random seed is used, the following matches the results in OP's self-answer: SeedRandom[1] dim = 3; mats = 4; n = 3; datab = RandomVariate[dist[2, 1], {n, mats, dim, dim}]; databt = RandomVariate[dist[2, 1], {n, mats, dim}]; datab == data > True TeXForm[datab] >$$\tiny\left( \begin{array}{cccc} \left( \begin{array}{ccc} 0.449043 & 1.48137 & 0.48613 \\ 1.2932 & 1.19225 & 1.64987 \\ 0.782326 & 1.21024 & 0.962458 \\ \end{array} \right) & \left( \begin{array}{ccc} 0.596656 & 1.24579 & 0.538028 \\ 0.927759 & 1.18168 & 0.151963 \\ 0.438377 & 0.278683 & 0.740327 \\ \end{array} \right) & \left( \begin{array}{ccc} 1.10816 & 1.25299 & 0.737502 \\ 1.43155 & 1.08755 & 0.582804 \\ 0.969597 & 0.445523 & 1.05965 \\ \end{array} \right) & \left( \begin{array}{ccc} 0.72258 & 0.810115 & 1.33333 \\ 0.865783 & 0.462852 & 2.10634 \\ 1.07203 & 0.485768 & 2.10349 \\ \end{array} \right) \\ \left( \begin{array}{ccc} 0.96868 & 0.882563 & 0.882634 \\ 0.56402 & 0.484774 & 1.15524 \\ 1.10104 & 0.901214 & 1.70141 \\ \end{array} \right) & \left( \begin{array}{ccc} 0.85481 & 0.550826 & 1.26273 \\ 0.779351 & 0.758341 & 0.51416 \\ 0.164397 & 1.29721 & 0.815494 \\ \end{array} \right) & \left( \begin{array}{ccc} 1.05785 & 1.26957 & 0.784296 \\ 0.876061 & 1.13112 & 0.775078 \\ 0.750277 & 0.782584 & 0.38401 \\ \end{array} \right) & \left( \begin{array}{ccc} 0.636682 & 1.13854 & 0.666643 \\ 0.596915 & 1.05141 & 1.80751 \\ 1.5429 & 0.625666 & 0.935543 \\ \end{array} \right) \\ \left( \begin{array}{ccc} 0.182974 & 0.560791 & 1.04689 \\ 0.426948 & 0.308514 & 0.603491 \\ 1.26573 & 0.401616 & 0.632274 \\ \end{array} \right) & \left( \begin{array}{ccc} 1.22939 & 0.869507 & 1.23884 \\ 0.738202 & 0.753391 & 2.00657 \\ 0.240648 & 0.722363 & 1.55349 \\ \end{array} \right) & \left( \begin{array}{ccc} 1.35294 & 0.717737 & 1.27567 \\ 0.806159 & 1.00854 & 1.46412 \\ 1.40222 & 1.20532 & 0.663839 \\ \end{array} \right) & \left( \begin{array}{ccc} 1.81562 & 0.775681 & 1.38224 \\ 0.843356 & 1.44941 & 0.452277 \\ 0.79226 & 1.07275 & 0.771253 \\ \end{array} \right) \\ \end{array} \right)$$ nearestvalueF = Map[Nearest, datab, {3}]; nearestvalues = MapThread[#[#2][[1]] &, {nearestvalueF, databt}, 3]; TeXForm[nearestvalues] >$$\tiny\left( \begin{array}{cccc} \{0.449043,1.19225,0.782326\} & \{0.538028,1.18168,0.740327\} & \{0.737502,1.08755,1.05965\} & \{1.33333,0.865783,0.485768\} \\ \{0.96868,0.484774,0.901214\} & \{0.85481,0.51416,0.164397\} & \{1.26957,1.13112,0.38401\} & \{0.636682,1.05141,0.935543\} \\ \{0.182974,0.426948,1.26573\} & \{1.22939,0.738202,0.240648\} & \{0.717737,1.46412,1.20532\} & \{1.38224,0.452277,0.771253\} \\ \end{array} \right)$$ rankofnearestF = Map[Nearest[Sort[#] -> Automatic] &, datab, {3}]; ranksofnearest = MapThread[#[#2][[1]] &, {rankofnearestF, databt}, 3]; TeXForm[ranksofnearest] >$$\left( \begin{array}{cccc} \{1,1,1\} & \{1,3,3\} & \{1,2,3\} & \{3,2,1\} \\ \{3,1,1\} & \{2,1,1\} & \{3,3,1\} & \{1,2,2\} \\ \{1,2,3\} & \{2,1,1\} & \{1,3,2\} & \{2,1,1\} \\ \end{array} \right)$$ combined = MapThread[List, {nearestvalues, ranksofnearest}, 3]; With this result we can match `rmin`, `rmedian` and `rmax` in OP's self-answer is preceded with `SeedRandom[1]`: Sort@Flatten@Pick[combined[[All, All, All, 1]], combined[[All, All, All, 2]], 1] == rmin > True Sort@Flatten@Pick[combined[[All, All, All, 1]], combined[[All, All, All, 2]], 2] == rmedian > True Sort@Flatten@ Pick[combined[[All, All, All, 1]], combined[[All, All, All, 2]], 3] == rmax > True Sort@Tally@Flatten@MapThread[#[#2][[1]] &, {rankofnearestF, databt}, 3] > {{1, 18}, {2, 9}, {3, 9}} Length /@ {rmin, rmedian, rmax} > {18, 9, 9} Legended[MatrixForm[Map[Style[#[[1]], (#[[2]] /. {1 -> Red, 2 -> Green, 3 -> Blue})] &, combined, {-2}], TableDirections -> Row], Placed[Column[Style @@@ Transpose[{{"rank", "first", "second", "third"}, {{Black, 16}, Red, Green, Blue}}], Dividers -> {False, 2 -> True}], Right]] [![enter image description here][1]][1] **Note:** In versions 10+, you can get the nearest values and their ranks using a single `Nearest` function: nearestValuesAndRanksF = Map[Nearest[Sort[#] -> {"Element","Index"}] &, datab, {3}]; combined2 = MapThread[#[#2][[1]] &, {nearestValuesAndRanksF, databt}, 3]; combined2 == combined > True ---------------- **Original answer:** data1 = Table[Table[Table[ Nearest[Sort@data[[k, i, j]] -> Automatic, RandomVariate[dist[2, 1]]][[1]], {j, 1, 3}], {i, 1, 4}], {k, 1, 3}]; TeXForm @ data1 >$$, \left( \begin{array}{cccc} \{1,1,2\} & \{1,1,1\} & \{3,3,1\} & \{1,2,3\} \\ \{1,2,3\} & \{2,3,2\} & \{1,1,2\} & \{3,3,3\} \\ \{1,3,2\} & \{3,3,2\} & \{2,1,3\} & \{3,1,3\} \\ \end{array} \right)$$ In versions 10+, you can also use `"Index"` instead of `Automatic` in the first argument of `Nearest`. **Update:** a cleaner version without nested `Table`s: datab = RandomVariate[dist[2, 1], {3, 4, 3, 3}]; nfs = Map[Nearest[Sort[#] -> Automatic] &, data, {3}]; databt = RandomVariate[dist[2, 1], {3, 4, 3}]; MapThread[#[#2][[1]] &, {nfs, databt}, 3]// TeXForm >$$ \left( \begin{array}{cccc} \{1,1,3\} & \{1,1,1\} & \{3,2,3\} & \{1,2,3\} \\ \{1,3,3\} & \{1,2,2\} & \{1,1,2\} & \{2,3,3\} \\ \{2,3,3\} & \{3,3,1\} & \{2,3,3\} & \{2,2,2\} \\ \end{array} \right)$$ [1]: https://i.sstatic.net/0BsOd.png