how to build a histogram from an output with 2 vectors

1 回表示 (過去 30 日間)
Heg Vel
Heg Vel 2020 年 10 月 13 日
コメント済み: Heg Vel 2020 年 10 月 18 日
I have a program that does the best selection from a file and choose the best trains to fit the weight=600 Tons and the highest nr of passengers. I want to execute the code multiple times and save output to create a histogram.
%display results:
if selection == zeros(chromosome_length, 1)
message = sprintf('GA CANNOT FIND VALID SELECTION WITH GIVEN CONSTRAINTS');
disp(message)
else
message = sprintf('OPTIMAL SELECTION OF ITEMS: [');
for i = 1:chromosome_length
if selection(i) == 1
message = sprintf('%s \n\t- %s', message, string(trains_table.Manufacturer(i)));
end
end
fprintf('%s\n ]\n', message);
fprintf('TOTAL weight OF RAILCARS: %d Tons\n', selection * trains_table.weight);
fprintf('TOTAL DAILY PASSENGERS: %d\n', selection * trains_table.daily_passengers);
end
Output is this:
Best fitness for this run = 13400
****GA Finished****
OPTIMAL SELECTION OF ITEMS: [
- Paynes Trains Limited Co.
- Motor Company of Maine
- Thomas The Engine Inc.
]
TOTAL weight OF RAILCARS: 600 Tons
TOTAL DAILY PASSENGERS: 13400

回答 (1 件)

Athul Prakash
Athul Prakash 2020 年 10 月 16 日
Hi Heg,
It seems that you haven't posted the code where the 'ga' algorithm is running and the results are obtained.
In general, I would recommend that you save the entire code to a file as a simple function which returns these outputs.
You can call the function multiple times in a loop to run 'ga' and populate a vector with the output of each run. Once you have a vector of all the outputs, the hist() function should help you create the desired histogram.
Hope it helps!
  5 件のコメント
Athul Prakash
Athul Prakash 2020 年 10 月 18 日
If you're new to Matlab or want to brush up on the basics of the language, such as functions, loops, vectors etc, I would suggest going through the Matlab OnRamp course online. It's a great resource and may get you up to speed in short time.
Heg Vel
Heg Vel 2020 年 10 月 18 日
Thank you so much!

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by