common random number when satisfying certain conditions

조회 수: 2 (최근 30일)
Fernando
Fernando 2013년 3월 20일
Hi all,
I'm trying to generate a matrix of random numbers that has to satisfy the restriction that when certain conditions are satisfied, the random numbers must be the same. Consider the following example with two individuals (id takes the values 1 and 2) and up to six options (defined by ch)
id=[1 1 1 1 1 1 2 2 2 2 2 2]';
ch=[1 2 3 1 2 3 3 4 5 3 5 6]';
I would like to generate random numbers such that whenever id==i and ch==j, the numbers are the same (and the same is true for all values of i and j).
I was able to do this in the following way
RN=zeros(12,5);
U=unique(id);J=unique(ch);
RNo=randn(kron(max(size(U)),max(size(J))),5); %for each combination I need 5 random numbers. Note that this generates random numbers that will never be used because there are some combinations of id and ch that do not appear in the data.
DJ=repmat(J,max(U),1); % Define a vector with all possible alternatives
DI=kron(U,ones(max(J),1)); % Define a vector with id repeated as many times as possible alternatives
for k=1:max(size(id))
ii=id(k,1);jj=ch(k,1);
for s=1:5,
RN(id==ii & ch==jj,s)=RNo(DI==ii & DJ==jj,s);
end
end
Now RN has the random numbers and these are the same whenever a combination of id and ch is repeated. However, doing this at a larger scale (my data consists of 500,000 observations that combine 900 values for id with up to 25 values for ch), with even one random number (instead of 5), takes forever.
Is there any way to generate the matrix with random numbers that does not require all this manipulation? I have tried to use indices but I haven't figured out a clever way to do it.
Thanks,
Fernando
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 3월 20일
50000 observations -- do you mean you want 50000 random numbers generated? 900 * 25 < 50000, so is the task to verify that the observations meet the condition if id&ch being consistent ?
Fernando
Fernando 2013년 3월 20일
Hi Walter,
I have 900 individuals and up to 25 alternatives. Individuals are observed over a lot of periods, meaning that each id may be observed with the same ch several times. This generates the 500,000 observations.
I need to generate the 900*25 random numbers and then assign these numbers accordingly to id and ch, generating a vector of 500,000x1 of repeated random numbers. The numbers will be repeated every time a combination of id and ch is repeated. My problem is how to assign the 900*25 random numbers to the data, according to the combination of id and ch.
Thanks,

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 20일
편집: Walter Roberson 2013년 3월 20일
[uid, uida, uidc] = unique(id);
[uch, ucha, uchc] = unique(ch);
numid = length(uid);
numch = length(uch);
randmat = rand(numid, numch);
randobs = randmat( sub2ind([numid, numch], uidc, uchc) );
Note: the above will fail if the id and ch vectors are not the same length.
  댓글 수: 2
Fernando
Fernando 2013년 3월 20일
Awesome, thanks!
Walter Roberson
Walter Roberson 2013년 3월 20일
Sorry, I missed accessing randmat() at those locations; I have edited that into place.

댓글을 달려면 로그인하십시오.

추가 답변 (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