Combine for loop output??
Show older comments
Hi I'm writing some code for my homework that will allow the user to choose a number of lines they want to buy in a lottery. My code is generating the numbers in the desired range, it is outputting the numbers from a for loop for the specified number of times, ultimately i want the combined output to go into a table to display all of the results. The purpose of the exercise isn't to be sleek its to use a variety of functions in one piece of code. Here's what i have so far:
% I am producing a code that will allow the
% user to choose a desired number of lines required for a euromillions
% lottery draw (Max 7 per slip)
User select number of lines they would like
number_of_lines = menu('Hello there welcome to the lottery draw please select a number of lines you wish to purchase? Remember its £2.50 a line and 7 lines maximum per ticket', '1','2','3','4','5','6','7');
% function to generate number of lines selected
% row vectors containing limits for numbers
main_draw_numbers = (1:50)
lucky_stars = (1:12)
% preallocate the output array
main_draw = zeros(number_of_lines,5);
% preallocate the output array
stars_draw = zeros(number_of_lines,2);
for draw=1:number_of_lines
% increment counter
main_draw = sort(datasample(main_draw_numbers,5,'Replace',false));
stars_draw = sort(datasample(lucky_stars,2,'Replace',false));
full_draw = [main_draw stars_draw];
T = array2table(full_draw,...
'VariableNames',{'Ball_1','Ball_2','Ball_3','Ball_4','Ball_5','Lucky_star_1','Lucky_star_2'})
end
3 Comments
Jonathan Good
on 4 May 2018
Wick
on 4 May 2018
You're only adding one line to the table - not concatenating them together for each draw. I've edited my solution. See if you like it better.
Wick
on 4 May 2018
I see you edited the code so it combines the full and stars draws in the FOR loop. But if you don't cat the loops together you're still going to get a single line in your table.
Something like
full_draw = [];
for ...
full_draw = [full_draw; main_draw stars_draw];
end
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!