I am using Matlab and I need your help! Assume that the 8 neighbors of the location (x,y) are:
top-left (x-1,y-1); top-middle (x,y-1); top-right (x+1,y-1);middle-left (x-1,y); middle-middle(x,y); middle-right (x+1,y);bottom-left (x-1,y+1); bottom-middle(x,y+1); bottom-right (x+1,y+1);
With the following example (of A = magic(5)), I can only replace the numbers or 'let's say' pixel values at (x-1, y) and (x, y-1) with the value at (x, y). But, I can’t replace the pixel values at (x, y+1) and (x+1, y) with the value at (x, y).
Can any help me to understand why ? In addition, can you please help me to write a code that would replace the pixel values at the locations (x-1, y); (x, y-1); (x, y-1) and (x+1, y) with the value of (x,y) location – without affecting other locations not nearest or adjacent to (x,y)? Many thanks in advance!
A = magic(5); M1 = size(A,1); M2 = size(A,2); E = [5 20]; for x = 2:(M1-1) for y = 2:(M2-1) for k = 1:length(E) if E(k) == A(x,y) A(x-1,y) = A(x,y); A(x,y-1) = A(x,y); end end end end
The expected results should look like this (with reference to E).
A = 17 5 1 8 15 5 5 5 20 16 4 5 20 20 20 10 12 19 20 3 11 18 25 2 9
Best Answer