How to generate sample Randomly in Matlab
Show older comments
Hello everyone i hope you are doing well.
I have the following code which generate 4000 sample for a value of 200. and output is the if 1x4000
I want the value to be random from 1 to 1000. and save the result of each value in seperate row
for example value=1 it has 4000 samples in first row, after value=100 it has 4000 samples in second row
value= [200];
Numberofsample = [4000];
output = repmat(value,1,(ceil(sum(Numberofsample)/length(Numberofsample))));
Answers (1)
Benjamin Thompson
on 3 Mar 2022
0 votes
>> output = randn(1,4000);
>> size(output)
ans =
1 4000
12 Comments
Med Future
on 3 Mar 2022
Benjamin Thompson
on 3 Mar 2022
Are you creating a plot where some data is the x axis values (not random, if so what is it) and the random values are on the y axis?
Or if you are just asking for the result to be a column vector instead of a row vector, swap the order of arguments to randn.
Med Future
on 3 Mar 2022
Benjamin Thompson
on 3 Mar 2022
You use randi for a uniform distribution over a specific range, and then plot to plot the results:
>> output = randi(1000,1,4000);
>> figure, plot(1:4000,output);
Med Future
on 3 Mar 2022
Med Future
on 3 Mar 2022
Benjamin Thompson
on 3 Mar 2022
If you are looking for a row vector containing the same random number repeated 1000 times, that would be:
output = randi(100, 1,1) * ones(1,1000);
If this is not what you want please try some of these examples and see if you can make it look the way you want. If you have more problems please ask a question with more information, perhaps a small example of the larger data set you want to create.
Walter Roberson
on 3 Mar 2022
Edited: Walter Roberson
on 3 Mar 2022
value = [1, 100, 200];
output = repmat(randi(1000, 1, 4000), length(value), 1);
Med Future
on 3 Mar 2022
Walter Roberson
on 3 Mar 2022
value = [1, 100, 200];
output = repmat(randi(1000, length(value), 1), 1, 4000);
Med Future
on 3 Mar 2022
Walter Roberson
on 3 Mar 2022
The method remains the same. Take one random quantity for each "value" and repeat it out to the 4000 or whatever you need.
If the class data is quantized, then you can use a random integer in the range of 1 to the number of quantized values, and use that to index the original data, like
output = repmat( MyCategories(randi(length(MyCategories), length(value), 1), 1, NumberOfCopies);
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!