Array Correlation - compute array difference with its neighbor elements
1 view (last 30 days)
Show older comments
Hi,
Do you know the fastest way to compute array mean difference with its neighbor elements in a N x N matrix? and is there a possibility to perform for second neighbors, third and so on.
Suppose you have
a=[1 4 5 3 6 ; 8 10 0 9 11 ; 2 5 20 15 9 ; 8 12 4 6 0 ; 7 9 18 2 5]
so I want a code to give this result for mean difference of first neighbors:
d(1,1)=[(1-4)+(1-8)]/2, d(1,2)=[(4-1)+(4-5)+(4-10)]/3, d(2,2)=[(10-8)+(10-0)+(10-4)+(10-5)]/4 and etc. For second neighbors: dd(3,3)=[(20-2)+(20-9)+(20-5)+(20-18)/4].
thank you
0 Comments
Answers (1)
Walter Roberson
on 29 Oct 2017
mask = [0 1 0; 1 0 1; 0 1 0];
Ncount = conv2(ones(size(a)), mask, 'same');
d = double(a) - conv2(double(a), mask, 'same')./Ncount;
You can leave out the double() if you are sure that a will not be integer data type (images are usually integer data type.)
For second difference, use an appropriate larger mask such as
[0 0 1 0 0; 0 0 0 0 0; 1 0 0 0 1; 0 0 0 0 0; 0 0 1 0 0]
2 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!