Array Correlation - compute array difference with its neighbor elements
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2017-10-29
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!