How can't a matrix be generated randomly?

2 views (last 30 days)
My first attempt to generate the 100-by-2000 matrix with random numbers is simply using "rand" or "randi."
The three main steps involving random numbers generation in the script are as followed.
len=100;
R=2000;
y=zeros(len,R);
for n=1:2.1e7
N=(0:len:(R-1)*len)+randi(len,1,R); % Only one element in each column was randomly selected.
y(N)=-1+9*rand(1,R); % The selected element of y matrix was replaced with a random number uniformly distributed from -1 to 8.
A=find(deltaE>0); %Compare E values calculated from y values in each column
exp(-delta_E/k/T)>rand(1,length(A)); %Accept E values when it meets the criterion.
end
When this for-loop finished, I observed clearly that the y values in the last 200 to 500 columns fluctuated around zero or still are zero but the all other y values from column 1 to column 1500 vary between -1 and 8.
I don't understand why this happen. I expected all columns should vary between -1 and 8.
The goal I want to reach is that all the random number generating(random N, random y values, rand(1,length(A)) in each column is independent to each other. The calculation of E value in each column is a independent simulation (i.e. I want to do 2000 independent simulations simultaneously).
However,the present results I have is that some columns seems untouched.
How can I modify the script to accomplish my goal?
Thanks !

Accepted Answer

Kye Taylor
Kye Taylor on 2 Jul 2012
If I understand you correctly, each column represents a different trial. What does the iteration variable n (going from 1 to 2.1e7) represent? Also, the line of code
exp(-delta_E/k/T)>rand(1,length(A)); %Accept E values when it meets the criterion.
makes no assignment and seems to be independent of your matrix y... unless delta_E is calculated using y somehow?
Can you describe what your goals is in more detail?
In the meantime, the following code creates a 100-by-2000 double array... selects a random entry in each column and assigns a random number between -1 and 8 to that entry.
y = zeros(100,2000);
randomRowIndex = randi(100,1,2000); % select random row for each column
randomLinearIndex= sub2ind(size(y), randomRowIndex, 1:2000); % linear index
y(randomLinearIndex) = randi(10,size(randomLinearIndex)) - 2; % the random #s
  1 Comment
Peter Perkins
Peter Perkins on 2 Jul 2012
Perhaps even simpler, that last line could be
randi([-1 8],size(randomLinearIndex));

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!