I have a large matrix like this:

```
      [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    9    4    2    1   NA    7
[2,]    9    5    1    1    1    7
[3,]    3    5    1    5   NA   10
[4,]    9    4    2   14   NA    7
[5,]   18    4    4   24   NA   10
[6,]   24    5    4   30   NA   10
[7,]    6    3    1   31    1   10
[8,]    2    5    4   34    1    7
[9,]    4    5    2   34   NA    7
[10,]   4    5    2   34   NA    7

```

I count how many changes I have from 7 to 10 in the 6th column. But I count pairwise. For example it is only allowed to count each row only once (more precise the number in the 4th column must appear only once). Now in addition, I want to know if both values in the 5th row equal 1.

Let's do an example with the given matrix above:
I have a change in the 6th column from 7 to 10 in the rows 2 to 3. Therefore the count = 1. Next I have again a change from row 3 to 4. But since row 3 cannot be counted twice this change will not be counted (in other words in column 5 the value 5 would appear twice in my countings, which is not allowed).

Next I count a change from row 4 to 5: count = count + 1. Hence, count = 2 up until now. The next change would be from row 7 to row 8. In row 7 and in row 8 the values in the 5th column equal 1. Therefore I would get at the end:

```
count = 3

```

and thereof one pair has 1's in the fifth column:

```
countl = 1

```

Has anyone a suggestion how to do this?