Right, there is a way using SUM()
, IF()
, COUNT()
and using array formulas.
The basic formula that you could use is (assuming your top array is A1:F6
, including the headers, so the data is in A2:F6
):
{=SUM(IF($B$2:$F$2=$B2:$F2,1,0))/COUNT($B$2:$F$2)}
Using array formulas, you can make the IF()
function return 1 for a match, and 0 for a mismatch, iterating through each element in a row. SUM()
adds the matches up, and then dividing by the COUNT()
of the number of cells processed gives you your similarity index.
The example above is for the top USA/USA cell in your example, you can fill down with that, but each new diagonal needs adjusting to change the fixed row number to the new row. So the top of the UK column would be:
{=SUM(IF($B$3:$F$3=$B3:$F3,1,0))/COUNT($B$3:$F$3)}
The COUNT()
could be removed if you know how many columns/countries there are in advance.
Note: You do not type the curly braces. When you have finished entering the formula you have to press Ctrl-Shift-Enter whilst editing, to get those to appear and for the formula to be treated as an array formula. These formulas are often called CSE formulas for this reason (Ctrl-Shift-Enter).
Update:
You can do it with a single formula, filled over the cells, using INDIRECT()
and COLUMN()
as well.
{=SUM(IF(INDIRECT("$B$"&COLUMN(B2)):INDIRECT("$F$"&COLUMN(B2))=$B2:$F2,1,0))/COUNT(INDIRECT("$B$"&COLUMN(B2)):INDIRECT("$F$"&COLUMN(B2)))}
This uses the fact that the column number is the same as the row number, providing the transposition.
Update 2:
Actually, the COUNT()
can be eliminated and SUM()
replaced with AVERAGE()
as all matches are 1, the mean is the correct value.
So this works for all cells:
{=AVERAGE(IF(INDIRECT("$B$"&COLUMN(B2)):INDIRECT("$F$"&COLUMN(B2))=$B2:$F2,1,0))}
Update 3:
If you desparately need nothing to appear on the upper diagonal side, making a lower triangular block, then you can use an IF()
wrapping the above formula, checking to see if the column is greater than the row and making the cell blank in that case. You can then fill the entire block with the formula and it will look right, with no need for deleting.
{=IF(COLUMN(B2)>ROW(B2),"",AVERAGE(IF(INDIRECT("$B$"&COLUMN(B2)):INDIRECT("$F$"&COLUMN(B2))=$B2:$F2,1,0)))}