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.
Related
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
This question already has answers here:
Reshaping of Array in MATLAB
(3 answers)
Closed 7 years ago.
I have the following matrix:
50,60,55,67,70
62,65,70,70,81
72,66,77,80,69
I turn now the matrix into a vector but in row-major. This gives the following vector:
50,60,55,67,70,62,65,70,70,81,72,66,77,80,69
Now I would like to turn this vector into the same matrix as above. The problem is that reshape(matrix,[3,5]) does not work because Matlab operates column-major.
How can this be done efficiently (for large matrices)?
To solve this, use
reshape(matrix,[5,3]).'
First using reshape with row and column dimension swapped, you get a matrix with the right order but transposed, then using transpose you get the right output.
Having the control systems toolbox, you could also use vec2mat
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:
Generate random number with given probability matlab
(7 answers)
Closed 7 years ago.
How do you generate a matrix with a predefined probability of occurrence for each possible value? For example, I need to generate a 3-by-5 matrix consisting of only 1, 0 and -1, where the probability of occurrence of 1 is 0.25, the probability of occurrence of -1 is 0.25 and the probability of occurrence of 0 is 0.5?
The simple answer is:
round(rand(3,5)*2)-1
more generally you have to find a function that produces random variables according to your probability distribution. Then you can use reshape() to get the right array.
This question already has answers here:
Generating N numbers that sum to 1
(6 answers)
Closed 10 years ago.
How do I distribute probability randomly over n values in matlab?
If I have 128 vectors.
I want to assign a random probabilty to all of them such that the sum of all of them equals to 1.
e.g.
n=4
p1=0.37
p2=0.21
p3=0
p4=0.42
Depending on how random you need to be, Roger Stafford takes a more stringent approach.
You can just divide the vector by the sum of it's elements. For example, for a vector of length 4 you can do:
>> v = rand(4, 1);
>> v = v/sum(v)
v =
0.2951
0.3281
0.0460
0.3308
>> sum(v)
ans =
1.0000
Note, I am assuming you want uniformly distributed numbers, since you don't state what distribution you want in the question.