MATLAB: How to randomly select an element in adjacent entries in a matrix in for loop

adjacentfor loopMATLABrandom selection

here's my code:
% code

for j=1:tmax
for i=1:n
if mat(i)==0 && rand() < r*(1-(zerfre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)=mat(idx);
elseif mat(i)==1 && rand() < r*(1-(onefre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
elseif mat(i)==2 && rand() < r*(1-(twofre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
elseif mat(i)==3 && rand() < r*(1-(thrfre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
elseif mat(i)==4 && rand() < r*(1-(foufre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
elseif mat(i)==5 && rand() < r*(1-(fivfre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
end
last=mat;
end
% code
Basically I'm updating my initial matrix mat with probability rules. What the rule does is, with some probability, it's replacing the entry with another entry that's randomly selected from the rest of the entire matrix.
Instead of this, I need to randomly select an element from an adjacent neighbourhood of entries, then replace the old entry with that.
By adjacent I mean within the closest 10*10 entries.
How would I do this?
Thanks a lot for your help!

Best Answer

I can answer my own question in case others are looking for solutions!
go to link:
playing with indices will give you the immediate entries. So far this seems like the best option I can get.