To start, I have a situation where I have some matrix, for example
$$ A=\left[ \begin{matrix} 4&2&2&3&3\\ 2&3&1&2&3\\ 3&0&4&0&4\\ 1&4&1&1&2\\ 1&3&4&1&4\\ \end{matrix} \right] $$
and I would like to count how many adjacent elements there are. Adjacent elements can be up, down, left or right. For a pair to be valid the numbers have to have the same value. For example $\left(A_{1,2},A_{1,3}\right)$ is a valid pair because the are both $2$ and they are next to each other. I need a way to count the defined adjacent element pairs on a matrix of size $n$.
eg.
$$ \left[ \begin{matrix} 2&2&3\\ 3&2&3\\ 2&1&1 \end{matrix} \right] $$
There would be 4 pairs in this matrix.
I thought about converting the matrix into some sort of graph with "weighted vertices" however I had no clue on doing so. It would have then made it a matter of counting arcs. So how would one produce a function that takes in a matrix and spits out the number of pairs (by my definition) it contains?
I am unsure whether or not I have tagged this correctly.




x,x,xas two pairs. Try it and reply...
– ciao Jan 16 '14 at 21:28(Count[{Differences /@ #}, 0, Infinity] + Count[{Differences /@ Transpose[#]}, 0, Infinity]) &[yourMatrixHere]Count[Flatten@{Differences[a, {1,0}], Differences[a, {0,1}]}, 0], where the matrix isa. – Szabolcs Jan 16 '14 at 21:46