# Help with finding the average correlation coefficient between a matrix value and its surrounding neighbors?

3 views (last 30 days)
RB on 17 Jan 2020
Commented: John D'Errico on 18 Jan 2020
I have a double matrix with dimensions 500x500. The matrix has numerical and NaN values. So far I have been able to do the following. I loop through each matrix element. At the particular element, I extract the pixel and its neighbors. Neighbors consist of 8 surrounding elements or less if the element is on an edge or corner. Then, I exclude any NaN values because I don't want them included in the following calculations.
Here is where I am having trouble. Overall, I want to be able to show how well do these surrounding pixels "describe" the pixel of interest. To do so, I want to find the average correlation coefficient between the element of interest and its surroudning neighbors. That is, find the correlation coefficient between the element and each of its neighbors, then find that average. Then I want to replace the element of interest with this value. If the element of interest is NaN, I want to keep it NaN. I will end up with a 500x500 matrix with NaN and values that correspond to the average correlation coefficient between that element and its neighbors.
I am having trouble with matlabs correlation coefficient command for finding this number between two values. It keeps giving me a value of 1 for two numbers that are not similar. Right now, I am using a = corrcoef(A,B) to find the corr. coeff. for the element of interest and each surrounding neighbor. Is there something I am missing? Is there another statistical value other than corr. coeff. that I could potentially use in place of this?
Thank you SO much for any help at all!

#### 1 Comment

KALYAN ACHARJYA on 18 Jan 2020
For the following case, what would be the answer?
8 1 6
3 5 7
4 9 2

Jeff Miller on 18 Jan 2020
A correlation isn't designed to measure the similarity between two isolated values, like your given (i,j) element and each of its neighbors. To make it a bit more concrete, there is no such thing as the correlation between my height and my wife's height. One can compute a correlation of spouse heights across a whole group of couples, but not for each couple separately.
It sounds like you are trying to assess similarity, so why not just look at the numerical difference between each (i,j) element and each of its neighbors? If the neighbors are similar to one another, then their averages differences from each other will be a lot less than their average differences from all elements (or from randomly selected other elements).

#### 1 Comment

John D'Errico on 18 Jan 2020
I want to agree with Jeff here. I think you are trying to force a correlation coefficient to give you information it is not really designed to provide.
I also think his idea is a good one - so at each pixel, compute the difference between the pixel and the 8 nearest neighbors of that pixel. Then compute the standard deviation of those differences, and report it as a local measure of how similar the pixel is to its immediate neighbors. The result would be an array of standard deviations.
It should be doable in a fully vectorized fashion too.