how to select an element from a matrix with a particular probability, not randomly selecting

3 次查看(过去 30 天)
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.

采纳的回答

Edgaris Rhomer
Edgaris Rhomer 2013-4-15
Here's my brief explanation for what I did.
I used randsample(POPULATION,N,true,W). Instead of thinking of updating the matrix with probabilities, think of sampling from a list of weighted entries. Ha!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by