# Anybody knows how to do to this script goes faster?

1 view (last 30 days)
Paulo Cunha on 24 Jan 2022
Commented: Paulo Cunha on 25 Jan 2022
Hi, everybody.
I just would like to know if is possible make this script goes faster. It is part of one study about lottery, and it is taking too much time to process. Anybody can help me?
% In this lottery, 15 numbers out of 25 (1 to 25) are drawn. You can win
% specific prizes if you match 11 numbers, or 12 numbers, or 13 numbers,
% or 14 numbers, or 15 numbers.
b = 0; v = 0;
v = 1:25;
combinations = nchoosek(v,15); % matrix with 3268760 lines with all
% combinations possible to 15 numbers
% chosen between 1 and 25.
lottery = randi(25,2400,15); %simulates 2400 draws for this lottery
number_of_combinations = size(combinations,1); %3268760
number_of_draws = size(lottery,1); %2400
registers = zeros(number_of_combinations,7);
for b = 1:number_of_combinations
registers(b,1) = b;
end
won15 = 0; won14 = 0; won13 = 0; won12 = 0; won11 = 0;
hit = 0;
i = 0; j1 = 0; j2 = 0; k = 0;
for i = 1:number_of_combinations
for k = 1:number_of_draws
for j1 = 1:15
for j2 = 1:15
if combinations(i,j1) == lottery(k,j2) %check how many times each combinations
% won some prize considering all 2400 draws
hit = hit+1;
end
end
if hit == 15
won15 = won15+1;
registers(i,2) = k; %won the biggest prize in what draw?
registers(i,3) = won15; %won with 15 numbers how many times?
end
if hit == 14
won14 = won14+1;
registers(i,4) = won14; %won with 14 numbers how many times?
end
if hit == 13
won13 = won13+1;
registers(i,5) = won13; %won with 13 numbers how many times?
end
if hit == 12
won12 = won12+1;
registers(i,6) = won12; %won with 12 numbers how many times?
end
if hit == 11
won11 = won11+1;
registers(i,7) = won11; %won with 11 numbers how many times?
end
end
hit = 0;
end
won15 = 0;
won14 = 0;
won13 = 0;
won12 = 0;
won11 = 0;
end
In this code I check how many times each combination possible won considering all 2400 draws.
Att,
Paulo

Rik on 24 Jan 2022
Using ismember will already speed up your loop by a lot. The point is that you are analysing a lot of data, so it just will take a very long time. About 8 hours on my machine assuming constant speed.
% In this lottery, 15 numbers out of 25 (1 to 25) are drawn. You can win
% specific prizes if you match 11 numbers, or 12 numbers, or 13 numbers,
% or 14 numbers, or 15 numbers.
v = 1:25;
combinations = nchoosek(v,15); % matrix with 3268760 lines with all
% combinations possible to 15 numbers
% chosen between 1 and 25.
lottery = randi(25,2400,15);
number_of_combinations = size(combinations,1);
number_of_draws = size(lottery,1);
registers = zeros(number_of_combinations,9);
registers(:,1)=1:number_of_combinations;
won15 = 0; won14 = 0; won13 = 0; won12 = 0; won11 = 0;
tic
for n = 1:number_of_combinations
for m = 1:number_of_draws
hits=sum(ismember(lottery(m,:),combinations(n,:)));
switch hits
case 15
won15 = won15+1;
registers(n,2) = m;
registers(n,3) = won15;
case 14
won14 = won14+1;
registers(n,4) = won14;
case 13
won13 = won13+1;
registers(n,5) = won13;
case 12
won12 = won12+1;
registers(n,6) = won12;
case 11
won11 = won11+1;
registers(n,7) = won11;
end
end
break
end
t=toc*number_of_combinations;
t=[floor(t/(60*60)) floor(mod(t,60*60)/60) mod(t,60)];
fprintf('approximate duration: %d:%02d:%.1f\n',t)
approximate duration: 22:57:46.9
Paulo Cunha on 25 Jan 2022
Awesome!! Your solution really made the processing time shorter and I learned a lot with it. Thank you very much!

### Categories

Find more on Entering Commands 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!

Translated by