How to generate sample Randomly in Matlab

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)

>> output = randn(1,4000);
>> size(output)
ans =
1 4000

12 Comments

@Benjamin Thompson no Sir, i want to be random value on y(axis) like levels and 4000 are the samples
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.
@Benjamin Thompson x axis has number of samples=4000
and y is the random values between 1 to 1000
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);
@Benjamin Thompson you dont understand my point. you are doing wrong
The above code output is like that.
I want to have random values on y axis like i have only 200 now
and save the values in seperate row
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.
value = [1, 100, 200];
output = repmat(randi(1000, 1, 4000), length(value), 1);
@Walter Roberson not like that. let me explain it again.
i want a random value which is remain same for 4000 samples in 1 row
then second random value which is remain same for 4000 save in 2 row so on.
value = [1, 100, 200];
output = repmat(randi(1000, length(value), 1), 1, 4000);
@Walter Roberson Thanks for the answer.
I have to generate the data for other classes as well. Can i post another question?
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);

Sign in to comment.

Products

Release

R2021b

Asked:

on 3 Mar 2022

Commented:

on 3 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!