Hi all. I have a 1000 by 1000 matrix filled with integers ranging from 1 to 6. I want to update each element in the matrix with some probability to an adjacent element in the matrix.
How I choose that precise adjacent element, is based on the probability that I can calculate using the frequency of my original element (I'll explain more later) and on the submatrix surrounding the original element. I figured out how to grab a submatrix with periodic boundaries so that I can extract a submatrix consistently even along the edges.
But I can't figure out how to implement this probability rule in my code..which is, I think, to select an element with a probability. Not randomly selecting.
For example, lets say I have a 3 by 3 matrix with integer ranging from 1 to 6 A = [1 2 6; 4 4 3; 5 5 4]
A(1,1) is 1. so I calculate the frequency of 1 in A, p_1=1/9
I grab a 2 by 2 submatrix of A(1,1), which is then sub_a11=[1 2; 4 4].
Then with probability p=(1-(p_1)^2), an element in sub_a11 replaces the original A(1,1).
Following is a part of my code.
% if mat(ii,jj)==1 && rand() < r*(1-(onefrequency)^2); for x=-5:4; for y=-5:4; mat_sub(x+6,y+6) = mat(mod(ii+x,n)+1,mod(jj+y,n)+1); end end
mat(ii,jj) = mat_sub(x+6,y+6);
It's in a nested for loop. Basically here I made extra efforts to check whether the element I'm looking at is 1, 2, ... or 6. then I tried to implement the probability part here by writing "rand() < r*(1-(zerfre)^2)". Then two for loops are for grabbing a submatrix, then I think (?) I'm replacing the original element to an element chosen from the submatrix, which is not what I want to do.
How do I choose an element from a matrix with a particular probability? How would I write it in a code?
By the way, I don't need to check whether the element is 1,2, ...6 until later but I wasn't sure how I would do the probability without doing that.
any help is very much appreciated, thanks so much.