# Generate specific shape randomly using MATLAB

4 visualizaciones (últimos 30 días)
Med Future el 6 de Mzo. de 2022
Comentada: Image Analyst el 6 de Mzo. de 2022
Hello every one, I hope you are doing well
i have the following code,in which i have two thing. first is the val in which i have the value like 200 is the value and it repeat 120 times to complete the shape.
I want to do it randomly , like the value generate randomly between 1 to 1000 and num is also randomly between 1 to 500 . How can i generate it in MATLAB
val = [200,500,800,1000,800,900,700,300,600,150];
num = [120,400,200,400,300,450,200,400,500,400];
out4 = repelem(val,num);
scatter(1:length(out4),out4)
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
David Hill el 6 de Mzo. de 2022
Your explaination is not clear to me. A short example would be helpful.

Iniciar sesión para comentar.

### Respuestas (2)

Image Analyst el 6 de Mzo. de 2022
To generate fractional random numbers use rand(). To generate random integers use randi().
randomDoubles = 999 * rand(1, length(val)) + 1; % Between 1 and 1000
randomIntegers = randi([1, 500], 1, length(num)); % Between 1 and 500
Not sure what else you need to know beyond that. If you need something else, be very explicit and specific.
##### 2 comentariosMostrar NingunoOcultar Ninguno
Med Future el 6 de Mzo. de 2022
Like the picture below . i want to generate this shape with random values .
Like i have the value 200,500 300.. first i generate a array of this random value .
then repeat the value with another random value like 200 repeat 120 times
Med Future el 6 de Mzo. de 2022
@David Hill @Image Analyst I have explain the problem above . I want to generate that kind of shape randomly . now i am manually doing it using above code.

Iniciar sesión para comentar.

Image Analyst el 6 de Mzo. de 2022
Try this:
val = unique(sort(20 * randi(50, 10, 1)))
for k = 1 : length(val)
y = val(k);
num1 = randi(3500);
num2 = randi(4000);
plot([num1, num2], [y, y], 'b.-', 'LineWidth', 5, 'markerSize', 20)
hold on;
end
grid on
##### 10 comentariosMostrar 8 comentarios más antiguosOcultar 8 comentarios más antiguos
Med Future el 6 de Mzo. de 2022
@Image Analyst Sir thats wrong my whole array is zero .just one value is some nonzero digit.
Have you run my own code.
I will explain my code so you get idea
i have one value for example 200 which is repeated for some samples like 120 time.
after next value 500 which is repated 500 times,
I want this to be in random form.
Like i randomly picked first value for example 10. then it repeated random time between 500. and so on to make a full aray
Image Analyst el 6 de Mzo. de 2022
Sorry, I tried, but now I'm heading out now to do some errands. Once you take that 2 hour training you'll be able to do it yourself. It sounds like it's a simple application of calling rand() or randi() and then using a for loop, something you'll be able to do after taking the 2 hour training. Good luck.

Iniciar sesión para comentar.

### Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by