How can i generate data randomly in MATLAB

Hello Everyone, I hope you are doing well
I have the following code. which generate 5 level of different values.as you can see in plot
I want to generate random level upto 32 for random value from 1 to 1000
How can i do that in matlab
VALUE = [100 300 600 700 1000 ];
out2 = repmat(VALUE,1,ceil(4000/numel(VALUE)));
scatter(1:length(out2),out2)

Respuestas (1)

try this:
VALUE=1:(1000-1)/31:1000
VALUE = 1×32
1.0000 33.2258 65.4516 97.6774 129.9032 162.1290 194.3548 226.5806 258.8065 291.0323 323.2581 355.4839 387.7097 419.9355 452.1613 484.3871 516.6129 548.8387 581.0645 613.2903 645.5161 677.7419 709.9677 742.1935 774.4194 806.6452 838.8710 871.0968 903.3226 935.5484
out2 = repmat(VALUE,1,ceil(4000/numel(VALUE)));
scatter(1:length(out2),out2);

14 comentarios

like this code but i want to save results row
Now my result of each row is over writting
level=randi(5, 1, 3);
for i = 1:length(level)
prf = rand(1,level(i))*1000;
out2 = repmat(prf,1,ceil(1000/numel(prf)));
end
scatter(1:length(out2),out2)
Med Future
Med Future el 4 de Mzo. de 2022
Now my result of each row is over writting. i want to avoid overwritting
and each row should be shape of 1x1000
level=randi(5, 1, 3);
out2 = cell([],1) ;
for i = 1:length(level)
prf = rand(1,level(i))*1000;
out2{i} = repmat(prf,1,ceil(1000/numel(prf)));
end
Med Future
Med Future el 4 de Mzo. de 2022
@KSSV i dnt want cell array i want numeric array
I suspect more like
VALUE = sort(randi([100 1000], 1, 32));
out2 = repmat(VALUE,1,ceil(4000/numel(VALUE)));
scatter(1:length(out2),out2);
@Walter Roberson i have the code its working
i just want to save results in each row
and row have shape of 1x1000
level=randi([2 32], 1, 100);
for i = 1:length(level)
prf=randi([1 1000], 1,level(i));
out2= repmat(prf,1,ceil(1000/numel(prf)));
end
Med Future
Med Future el 4 de Mzo. de 2022
Editada: Med Future el 4 de Mzo. de 2022
@KSSV @Walter Roberson some time the shape is 1x1023, 1x1051
i want to shape of each row 1x1000 and save results row wise
i have used the following code but it gives the following err
Unable to perform assignment because the size of the left side is 1-by-1005 and the size of the right side is 1-by-1000.
level=randi([2 32], 1, 100);
[M,N]=size(level);
for j = 1:size(level,2)
prf=randi([1 1000], 1,level(j));
size(prf)
out2(j,:)= repmat(prf,1,ceil(1000/numel(prf)));
end
Walter Roberson
Walter Roberson el 4 de Mzo. de 2022
Your code asked for ceil(4000/32) each, not 1000 each. And the code I posted would not create a different number for each value.
Now this code is working, I want to reshape it into 1x1000
every row has 1000 samples
Can you please help me
level=randi([2 32], 1, 100);
[M,N]=size(level);
for j = 1:size(level,2)
prf=randi([1 1000], 1,level(j));
size(prf);
out = repmat(prf,1,ceil(1000/numel(prf)));
out2(j,1:length(prf)*ceil(1000/numel(prf)))=out;
end
Med Future
Med Future el 4 de Mzo. de 2022
@Walter Roberson what can i change in this out = repmat(prf,1,ceil(1000/numel(prf))); command to generate row of 1x1000
Walter Roberson
Walter Roberson el 6 de Mzo. de 2022
You cannot do that using repmat. For example if 17 were generated for level you would be asking what integer number of copies of 17 elements can you take to get exactly 1000. And you just cannot do that, as 17 does not divide exactly into 1000. But you can use the out that you are currently generating with that code and take the first 1000 entries using indexing.
Med Future
Med Future el 6 de Mzo. de 2022
@Walter Roberson so i cant reshape it into 1000?
level = 17; %randi([2 32], 1, 100);
[M,N]=size(level);
for j = 1:size(level,2)
prf=randi([1 1000], 1,level(j));
size(prf)
out = repmat(prf,1,ceil(1000/numel(prf)))
whos
end
ans = 1×2
1 17
out = 1×1003
941 438 290 294 399 989 876 941 501 786 405 23 422 262 95 118 716 941 438 290 294 399 989 876 941 501 786 405 23 422
Name Size Bytes Class Attributes M 1x1 8 double N 1x1 8 double ans 1x2 16 double j 1x1 8 double level 1x1 8 double out 1x1003 8024 double prf 1x17 136 double
Notice that out is length 1003. You cannot reshape() 1003 into 1000.
What you can do is
out = out(1:1000);
and you could reshape() that

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2021b

Preguntada:

el 4 de Mzo. de 2022

Comentada:

el 6 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by