How to add the amount of two generated values in a graph?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2022-11-9
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?
采纳的回答
Voss
2022-11-10
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'})
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
