Array Correlation - compute array difference with its neighbor elements
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Walter Roberson
am 29 Okt. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!