Generate specific shape randomly using MATLAB

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

Your explaination is not clear to me. A short example would be helpful.

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2022년 3월 6일
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

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
@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.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2022년 3월 6일
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

@Image Analyst why you just ploting that? i want to be it in a numeric array
@Image Analyst i want to generate the a large dataset. which is randomly like the above shape
I plotted that because that's what you said you wanted: "Like the picture below . i want to generate this shape with random values ."
How many elements do you want there to be between the left endpoint of the line segment and the right endpoint? You can use
numVec = linspace(num1, num2, numberOfElements);
A good investment of your time:
@Image Analyst i want it to in array. plotting is just an option.
like the code you share below how can i save this results in Array to generate a dataset like above random shapes
val = unique(sort(20 * randi(50, 10, 1)))
for k = 1 : length(val)
y = val(k);
num1 = randi(3500);
num2 = randi(4000);
@Image Analyst Have you get the idea what i want to do ?
Again, "How many elements do you want there to be between the left endpoint of the line segment and the right endpoint?"
And do you want each value of val to be in its own row? Or maybe you want this:
val = unique(sort(20 * randi(50, 10, 1)))
m = zeros(length(val), 4000)
for k = 1 : length(val)
y = val(k);
num1 = randi(3500);
num2 = randi(4000);
index1 = min([num1, num2]);
index2 = max([num1, num2]);
plot([num1, num2], [y, y], 'b.-', 'LineWidth', 5, 'markerSize', 20)
hold on;
m(k, index1 : index2) = 1;
end
grid on
Med Future
Med Future 2022년 3월 6일
편집: Med Future 2022년 3월 6일
@Image Analyst let me explain.
I want the data that each row contain 1x1000 shape like 1000 samples in each row
i want 200 rows like that data in which shapes are randomnly generated and saved in a array .
Like the above code i have share in which a single row have shaped of 1x3370.
I want it to be randomnly generate
Try this:
val = sort(randi(1000, 200, 1));
m = zeros(length(val), 1000);
for k = 1 : length(val)
y = val(k);
num1 = randi(3500);
num2 = randi(4000);
index1 = min([num1, num2]);
index2 = max([num1, num2]);
plot([num1, num2], [y, y], 'b.-', 'LineWidth', 3, 'markerSize', 20)
hold on;
indexes = round([index1, index2] / 4) % Now go from 1-1000 instead of 1-4000
m(k, indexes) = y; % m is a 200 by 1000 array
end
grid on
Now you have a 200 row array where the value of the y height goes from index1 to index2. There are 1000 columns which indicate x going up to 4000.
@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
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2022년 3월 6일

댓글:

2022년 3월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by