# From original dataset, find the probability of each element in each row having the maximum value in its row

Suppose I have this dataset

{{1, 3, 5}, {4, 4, 2}, {5, 3, 5}, {10, 4, 10}} // MatrixForm


How do I find the probability that each cell is the maximum in its row.

So for example in the first row,

{1, 3, 5}


The probability of the first element being max is 0 (since its value is 1 while the third element has value 5). The probability of the second element being max is 0 (since its value is 3 while the third element has value 5). The probability of the third element being max is 1 (since its value is 5).

For the second row:

{4,4,2}


The probability of the first element being max is 1/2 (since its value is 4 equal to the second element). The probability of the second element being max is 1/2 (since its value is 4 equal to the first element). The probability of the third element being max is 0 (since its value is 2 while the first and the second elements have value 4).

Thanks all!

• As @JimB has nicely explained below you are using the term probability, but what are are really talking about is relevance or degree of truthfullness (e.g., as in a fuzzy membership function indicating the degree of belonging to the set of unique row maxima). That is not the same and it would be better to not talk about probability here.
– gwr
Commented Aug 12, 2022 at 16:56

I am sure there are lots of ways to do this. One could be a direct approach. Find the max of each row. Then iterate over each row. if element is not max, give zero, else 1 divided by number of maximum elements in that row.

Using MapIndex to match the max of each row with the current row being iterated over.

mat = {{1, 3, 5}, {4, 4, 2}, {5, 3, 5}, {10, 4, 10}};
max = Map[Max[#] &, mat];
f[row_List, max_?NumericQ]:= With[{z = Count[row, max]}, If[# != max, 0, 1/z] & /@ row];
MapIndexed[f[#, max[[First[#2]]]] &, mat]


• Thanks, very helpful. I know it is not that difficult, but I just didn't know which library to look up. I will check MaxIndexed now. Commented Aug 12, 2022 at 5:50

We first solve the problem for a single list (a vector). It is then easy to apply it to a list of lists (a matrix).

A single list. One can use

f[a_List] := Thread[a==Max[a]] /. {True->1,False->0} // #/Total[#]&


For example

f[{4,4,2}]
(* {1/2,1/2,0} *)


A list of lists. To apply f to each row, use Map[f,...] as in

Map[f,{{1,3,5},{4,4,2},{5,3,5},{10,4,10}}]
(* {{0,0,1},{1/2,1/2,0},{1/2,0,1/2},{1/2,0,1/2}} *)


You could write

f = #/Total[#, {2}] &[UnitStep[# - Max /@ #]] &;
f[list]
(* {{0, 0, 1}, {1/2, 1/2, 0}, {1/2, 0, 1/2}, {1/2, 0, 1/2}} *)


This is an extended comment to challenge you and the other folks giving answers so far.

I’m going to attempt to argue that your question either makes no sense or at least is incomplete for two reasons.

1. You have not stated anything about the probabilistic process of arranging items in a row or if the probabilistic process is about how a cell in a row is selected. Therefore, given a dataset, the probabilities will be either 0 or 1.

2. The assignment of a probability of 1/2 makes no sense unless you add in a more specific definition of your objective and a description of the probabilistic process. For example, that might be “the probability that each cell is the only cell equal to the maximum in its row” (which still only allows probabilities of zero or one) or “the probability that a randomly selected cell equals the maximum in its row”. If the number of items in a row is $$n$$ and the number of cells with the same value of the maximum is $$n_{\text{max}}$$, then with the latter definition the probability of a randomly selected cell having the maximum value is $$n_{\text{max}}/n$$. For rows with 3 cells (as in your example, that probability can only be 1/3, 2/3, or 1 and not 1/2.

In short you've described no probabilistic process and that is essential.

la = {{1, 3, 5}, {4, 4, 2}, {5, 3, 5}, {10, 4, 10}};


Using Rescale and Boole

Rescale[#, {0, Count[#, 1]}] &[Boole @ Thread[# == Max[#]]] & /@ la


Another test

lb = {{2, 2, 2, 2}, {2, 2, 2, 1}, {2, 2, 1, 1}, {2, 1, 1, 1}};

 Rescale[#, {0, Count[#, 1]}] &[Boole @ Thread[# == Max[#]]] & /@ lb