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

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!

4 Comments

To me it doesn't look like your code does anything like that. Can you explain what the difference is between the various "if" cases? And why you only do one of them if mat(i) is from the set [0,1,2,3,4,5]? Why should the value of mat(i) matter if you're just going to replace it with some random element within +/- 5 of index i?
Hmm.
the different "if" cases are looking whether the mat(i) entry is either 0,1,2,3,4 or 5. I don't understand 100% of the following line in the code:
rand() < r*(1-(zerfre)^2)
either, all I wanted to say was that the old entry will be replaced by a new entry with some probability r*(1-(zerfre)^2) from the rest of the entire matrix.
It's because later on I'll have different probabilities for different entries, which I am hoping to update to bit-strings instead integers like I have right now.
Does this answer all of your questions? I don't understand what you mean by the last question, by the way.
If I'm processing element 85 and I want to replace it with some other element from 75 to 95, taken at random, then what does it matter if mat(85) is 3 or 1 or pi or 42 or whatever? Who cares what it is? Yet you check for it.
I haven't coded this yet and I guess thats why you're not satisfied with my code. Later on it's going to matter to me if mat(85) is 3 or 1, because what I'm ultimately interested in seeing is the rise of a majority language, (i.e. the most frequent entry) and how a special minority language like French is doing. Later on, what I hope to do is I'll detect some clusters of French, then I'll reduce the switching probability so that French is more likely to stay there than other languages, etc.

Sign in to comment.

 Accepted 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.

More Answers (0)

Categories

Find more on Psychology in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!