This question already has answers here:
Matlab, matrix containing random numbers within specified range
(3 answers)
Closed 5 years ago.
I'm new to matlab and trying to learn how to simulate random numbers.
Is there a simple way to create a 10x20 array filled with random numbers from the uniform (-1,1) distribution.
I've seen the rand function, but I'm not sure how to change the uniform (0,1) distribution to (-1,1).
We actually CAN just deform interval of (0,1) using different math operations and still get uniform distribution.
So you have to go this way:
result = rand(10,20).*2-1
To check is it really still uniform lets do the next:
res = rand(10000).*2-1;
histogram(res)
As you can see it is still uniform.
There are some tricks about random numbers (actually it's pseudorandom) and you can get the same 'random' results after restart MATLAB. read about this here and here.
You can use the following formula given in Matlab rand documentation to achieve random numbers between any interval of numbers:
r = a + (b-a).*rand(N,1)
In your case for (-1,1) interval and an array sized (10,20) it should be:
r = -1 + (1+1).*rand(10,20)
References:
rand Matlab official documentation
Related
This question already has an answer here:
Generating a random complex vector in matlab
(1 answer)
Closed 9 months ago.
I want a matrix all filled with complex numbers and it would be better if it's a square matrix.
In order to generate random complex numbers in a matrix, you can do
A = (randi(3,3)) + (randi(3,3))*i*(randi(4))
A = (rand(3,3)) + (rand(3,3))*i*(rand(4))
rand would give real numbers whereas randi would only spew out integer values.
Here I have chosen a 3*3 matrix and imaginary part's magnitude will always be within 4.
This question already has answers here:
Weighted sampling without replacement in Matlab
(5 answers)
Weighted random numbers in MATLAB
(4 answers)
Closed 7 years ago.
I'm trying to calculate 5 numbers at random. The numbers 1-35 have set probability weights that are assigned to each number. I'm wondering how, in Matlab, to compute 5 random numbers with weights WITHOUT repeats. Also seeking how to compute 5 sets of those.
Although I would suspect MATLAB has a built in function for this, the documentation for randsample suggests otherwise:
Y = randsample(N,K,true,W) or randsample(POPULATION,K,true,W) returns a
weighted sample, using positive weights W, taken with replacement. W is
often a vector of probabilities. This function does not support weighted
sampling without replacement.
So, instead, since you only are looking for a few numbers, looping isn't a terrible idea:
POP = 1:35;
W = rand(1,35); W=W/sum(W);
k = 5;
mynumbers = zeros(1,k);
for i=1:k
mynumbers(i) = randsample(POP,1,true,W);
idx2remove = find(POP==mynumbers(i));
POP(idx2remove) = [];
W(idx2remove) = [];
end
The entries in W are your weights. The vector POP is your number 1 through 35. The number k is how many you'd like to choose.
The loop randomly samples one number (with weights) at a time using MATLAB's randsample, then the selected number and corresponding weight are removed from POP and W.
For larger k I hope there's a better solution...
This question already has answers here:
Weighted random numbers in MATLAB
(4 answers)
Closed 7 years ago.
I have to generate a random variable that ranges from 20 to 30,
with 0.1 interval,
and the pmf of it is given by 1x101 matrix (for x=20, 20.1, ... 29.9, 30).
Now I have to make random variable with this given pmf,
But all I know about generating R.V is something related to gaussian, or uniformly distributed function.
So is there any way to implement generating random variables with arbitrary pmf?
I think this should do the trick.
%// First I created some random data
N=1e6;
x=randn(1,N);x=(x-min(x))/(max(x)-min(x));
pmf=hist(x,101)./N;
%// It's a normal distribution but scaled to output to [0 1]
%// First, get the cdf
xh=cumsum(pmf);
%// Create the list of possible values of this random variable
Y=20:.1:30;
%// Create a uniformly distributed random variable between 0 and 1
R=rand()
%// Find where the cdf value is closest to R
[~,J]=min(abs(xh-R));
%// Find the resulting sample values
Y(J)
%// Just a little plot to make sure it's working
hold on
plot(Y,xh)
plot([20 30],[R R],[Y(J) Y(J)],[0 1])
hold off
I think what you are trying to do is multinomial sampling, see http://se.mathworks.com/help/stats/mnrnd.html?refresh=true
If you just want one sample, you can (crudely) do something like
find( mnrnd(1,pmf))
where pmf is the vector of probabilities (assert(sum(pmf)==1))
Note that there is nothing special of using 20:.1:30, you basically have 101 bins each with probability given by pmf
This question already has answers here:
Converting a Uniform Distribution to a Normal Distribution
(16 answers)
Closed 9 years ago.
I want to generate geometric or Gaussian distributed random numbers without using "geornd" or "randn" functions present in MATLAB library. How can I generate random numbers with those distributions by using only the "rand" function used to generate uniformly distributed random numbers.
I want to do this because Uniform distribution is the most basic type distribution and any other distribution can be generated from this distribution. A small code example would be very helpfull..!!
I suppose you can do this:
U = rand(1)
mu = 5;
sigma = 3;
N = mu + sigma * norminv(U,1,1)
This question already has an answer here:
PDF and CDF plot for central limit theorem using Matlab
(1 answer)
Closed 3 years ago.
I would like to use MATLAB to visualize the Central Limit Theorem in action. I would like to use rand() to produce 10 samples of uniform distribution U[0,1] and compute their average, then save it to a matrix 'Mat'.
I would then use a histogram to visualize the convergence in distribution. How would you do this and normalize that histogram so it is a valid probability density (instead of just counting the frequency of occurrence)?
To generate the samples I am doing something like:
Mat = rand(N,sizeOfVector) > rand(1);
But I guess I am going to the wrong side.
To generate N samples of length sizeOfVector you start out with rand as you suggested, and then continue as follows (calling the array average instead of Mat for readability):
samples = rand(N,sizeOfVector);
average = mean(samples,1);
binWidth = 3.49*std(average)*N^(-1/3)); %# Scott's rule for good bin width for normal data
nBins = ceil((max(average)-min(average))/binWidth);
[counts,x] = hist(average,nBins);
normalizedCounts = counts/sum(counts);
bar(x,normalizedCounts,1)