Search code examples
excelmatrixexcel-formulaadjacency-matrix

Excel How do I fill a matrix by MAXIFS comparison


I have this dataset:

Groups   A A B B
location a b c d
         3 4 0 5

I also have a transformed version for better clarification:
Groups location
 A        a     3
 A        b     4
 B        c     0
 B        d     5

What I want is a simple matrix that fills binary.

The function should check each row and column wether it has the MAXIF from it's respective group and then compare it to the second value, which is a second MAXIF from it's group. Therefor the combination b and d has to resolve to 1.

The intended output is as following: I have a dataset with a-n locations that are grouped in a-n groups. So group A has the locations a,b; group B the locations c,d. The columns represent different features at each location. I want to build a matrix out of it, but not the "usual" distance matrix but one that incorporates the following questions: -When building the matrix, the maximum values of each group get compared -I want to find out, if the value I am looking at is the maximum value in this group and if so, compare it to the second groups maximum -> if this number is larger -> set it to 1 -this should automatically fill all fields in the matrix

I need this for a network analysis of my data, to wipe out not needed connections

My current input is somewhat like this:

=IF(AND(>0(MAXIF()=value)>(AND(>0(MAXIF()=value);1;0)

How it looks like in excel:
=IF(AND(A$1<>$A7; A$3>0;(MAXIFS($A$3:$D$3;$A$1:$D$1;A$1)=A$3))<(AND(A$1<>$A7; $C7>0;MAXIFS($C$7:$C$10;$A$7:$A$10;$A7)=$C7));1;0)

However I think internally it does not actually compare values but TRUES and FALSE. Therefore connections that are smaller than MAX are getting 1s. My output currently:

    A A B B
    a b c d
A a 0 0 0 0
A b 0 0 1 0
B c 0 0 0 0
B d 1 0 0 0

As you can see, the value a and d resolve to 1.

The output should look like this: (the matrix is generally speaking 0, but when beacons like d (5) and b (4) meet, it gets "1" since both are the highest within their group. Only here's a connection between the two groups.

    A A B B
    a b c d
A a 0 0 0 0
A b 1 0 0 1
B c 0 0 0 0
B d 0 1 0 0

I understand the problem but don't know how to fix that.


Solution

  • I'm fairly sure this doesn't work properly, but it may help. I've restructured your data slightly to make it easier to write the formula.

    The formula in C3 etc is

    =IF(AND(MAXIFS($G$3:$G$6,$A$3:$A$6,$A3)=$G3,MAXIFS($C$7:$F$7,$C$1:$F$1,C$1)=C$7,$G3>MAXIFS($G$3:$G$6,$A$3:$A$6,"<>" & $A3)),1,0)
    

    It's just checking if the value in G is the max for the group in column A, and if the value in row 7 is the max for group in row 1, and if the value in G is greater than the max of the other group. If they're all satisfied it inserts a '1'.

    enter image description here