# How should I implement a confusion matrix in Matlab?

I know what is a confusion matrix.

Given N classes we have a NxN matrix M where - each row is one of the classes - each column is one of the classes

M(X,Y) = number of elements which have been classified in class X and should have classified in class Y (of course if X=Y the classification is correct).

Now I have a set of "compound classes" and a variable number of classes

• with "COMPOUND" I mean that one class corresponds to a vector (of variable length). For example if the class is represented by a 2 element vector,the possible classes will be:

[0,0]

[0,1]

[1,0]

[1,1]

Mi goal is to define a function with:

• INPUT: a NxM matrix (where N=number of classified elements,M=number of elements of each class) of classified elements , a NxM matrix of expected class for each element.
• OUTPUT: NXN confusion matrix corresponding to input classes

Example of OUTPUT:

```[1 0 0]
[0 2 0]
[0 1 0]
```

This matrix is 3x3 matrix.

This means the the total number of classes is 3 and number of classified elements are 4:

• one element should have been classified in class 1 and has been classified in class 1
• two elements should have been classified in class 2 and have been classified in class 2
• one element should have been classified in class 3 and has been classified in class 2

(it doesn't matter how the elements are composed of. Let's image the classes may be:

```[0,0,1]
[1,0,1]
[1,1,1]
```

** Example of input matrix (EXPECTEDclasses): **

```[0,0,1]
[1,0,1]
[1,0,1]
[1,1,1]
```

** Example of input matrix (OBTAINED classes): **

```[0,0,1]
[1,0,1]
[1,0,1]
[1,0,1]   <-- this is the element(line) incorrectly classified
```

How should I do? (the classification is made by a Perceptron or Adaline neuronal network)

Thank you in advance for any hint!

The key is to use ismember in the following form: [Lia,Locb] = ismember(A,B,'rows') The second output argument tells you which row of matrix B that each row of matrix A matches. Use this information to construct the confusion matrix (which is a built-in function in the stats toolbox).

```classes = [0 0 1;
1 0 1;
1 1 1];
expected = [0 0 1;
1 0 1;
1 0 1;
1 1 1];
obtained = [0 0 1;
1 0 1;
1 0 1;
1 0 1];
>> [~, ex] = ismember(expected, classes,'rows')
ex =
1
2
2
3

>> [~, ob] = ismember(obtained, classes,'rows')
ob =

1
2
2
2

>> confusionmat(ex,ob,'order',[1 2 3])
ans =

1     0     0
0     2     0
0     1     0
```

If you don't have access to confusionmat, you can use accummarray to do it yourself.

```num_observations = length(ex);
num_classes = size(classes,1);
accumarray([ex,ob],ones(num_observations,1),[num_classes,num_classes])
```