How to add the amount of two generated values in a graph?

3 views (last 30 days)
The code below is what I currently have. I have an output for the density of the generated values but I'm looking for a way to display the number of small grain size and large grain sizes that are generated in a graph.
clc
clear
x = [];
taken_spots = [];
%% Generate Random Numbers
s = 10000; %total number of available parking spots
n = s*5; %n = quantity of random numbers to generate
L = .1; %chance for large grain = L (0-L = Large grain, > L = small grain)
LG = 10; %large grain size
SG = 3; %small grain size
numbers = rand(n,1); %n number of random numbers to be used for vehicle size
spot = round(s*rand(n,1)); %random starting spots for each vehicle between 1 and s spot limit
for i = 1:(n)
if numbers(i) >= 0 && numbers(i) <= L %if random number is within 0-L
Y(i) = LG; %assign the large grain size
end
if numbers(i) > L && numbers(i) <= 1 %if random number is above L
Y(i) = SG; %assign small grain size
end
end
%% Fill One Row
for i = 1:(n)
VL = Y(i); %find current vehicle length
if VL == SG %if the current particle is small size
x = [spot(i), spot(i)+1, spot(i)+2]; %generate potential new spots for small vehicle starting at random spot
Lia = ismember(x, taken_spots); %Lia returns an array of 0 for non-similar values and 1 for similar values
B = any(Lia); %if any values of Lia are 1 (repeated values) B is 1, else B is 0 and no spots are already taken
if B == 0
len = length(taken_spots); %find length of currently taken spots array
taken_spots(len+1:len+3) = x; %add new spots to end of taken spots array
end
end
if VL == LG %if the current particle is large size
x = [spot(i),spot(i)+1,spot(i)+2,spot(i)+3,spot(i)+4,spot(i)+5,spot(i)+6,spot(i)+7,spot(i)+8,spot(i)+9]; %generate potential new spots for large vehicle starting at random spot
Lia = ismember(x, taken_spots); %Lia returns an array of 0 for non-similar values and 1 for similar values
B = any(Lia); %if any values of Lia are 1 (repeated values) B is 1, else B is 0 and no spots are already taken
if B == 0
len = length(taken_spots); %find length of currently taken spots array
taken_spots(len+1:len+10) = x; %add new spots to end of taken spots array
end
end
end
density = length(taken_spots)/s*100
  2 Comments
Image Analyst
Image Analyst on 9 Nov 2022
Not sure what you're looking for. What is the array that holds the sizes of the grains? What is the difference between a grain, a spot, and a vehicle? Are you trying to fit vehicles (or grains of something) onto a number line where the objects have different lengths? Do you have any images or graphs that you could upload to explain the situation better?
Branson
Branson on 9 Nov 2022
The grain is the same as the vehicle. Vehicle is a term that is being used to represent the different sizes of grains, for example the large grain could be a bus, and the small grain could be a motercycle both with different lengths/sizes. Spot is the area that the vehicle/grain takes up on a line of length s. Yes, the objective is the try to fix different length vehicles on a line of s length with vehicles being placed in random spots of the line with no overlap. I don't have any images or graphs to better explain. But the graph would simply be a bar graph with 2 bars of the number of SG and LG that fit with random arivals.

Sign in to comment.

Accepted Answer

Voss
Voss on 10 Nov 2022
x = [];
taken_spots = [];
%% Generate Random Numbers
s = 10000; %total number of available parking spots
n = s*5; %n = quantity of random numbers to generate
L = .1; %chance for large grain = L (0-L = Large grain, > L = small grain)
LG = 10; %large grain size
SG = 3; %small grain size
numbers = rand(n,1); %n number of random numbers to be used for vehicle size
spot = round(s*rand(n,1)); %random starting spots for each vehicle between 1 and s spot limit
is_LG = numbers <= L;
Y = SG*ones(1,n);
Y(is_LG) = LG;
nLG = 0;
nSG = 0;
for i = 1:n
x = spot(i)+(0:Y(i)-1);
if ~any(ismember(x, taken_spots))
taken_spots(end+1:end+Y(i)) = x; %add new spots to end of taken spots array
end
if is_LG(i)
nLG = nLG+1;
else
nSG = nSG+1;
end
end
density = length(taken_spots)/s*100;
bar([nSG nLG])
xticklabels({'# small' '# large'})
  1 Comment
Branson
Branson on 15 Nov 2022
Thank you. I got this to work for me with a few changes. I was wondering if there was an easy way to make this code opperate for 2D instead of 1D. So instead of having a 3x1 and a 10x1 in a 10,000x1 array, having a 3x3 and 10x10 in a 10,000x10,000 array with the same conditionds where they cant overlap. The graph should work the same but the grains/vechicals should now be 2D instead on 1D.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!