Translate code to parallel computing

1 次查看(过去 30 天)
Hello, everybody
I just would like to ask if somebody could help me to "translate" the follow code to turn it able to run using the parallel computing toolbox:
It is part of one study about lottery, and it is taking too much time to process.
% 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.
thanks in advance,
Paulo

采纳的回答

Jan
Jan 2022-12-9
% 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;
comb = nchoosek(uint8(v), 15); % matrix with 3268760 lines with all
% combinations possible to 15 numbers
% chosen between 1 and 25.
lottery = randi(25, 2400, 15, 'uint8'); %simulates 2400 draws for this lottery
nComb = size(comb, 1); %3268760
nDraw = size(lottery, 1); %2400
% registers = zeros(nComb, 7, 'uint32');
% registers(:, 1) = uint32(1):uint32(nComb);
registers = cell(nComb, 1);
registers(:) = {zeros(1, 7, 'uint32')};
parfor i = 1:nComb
won15 = 0;
won14 = 0;
won13 = 0;
won12 = 0;
won11 = 0;
for k = 1:nDraw
hit = 0;
for j1 = 1:15
for j2 = 1:15
if comb(i,j1) == lottery(k,j2) %check how many times each combinations
% won some prize considering all 2400 draws
hit = hit+1;
end
end
switch hit
case 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?
case 14
won14 = won14+1;
registers{i}(4) = won14; %won with 14 numbers how many times?
case 13
won13 = won13+1;
registers{i}(5) = won13; %won with 13 numbers how many times?
case 12
won12 = won12+1;
registers{i}(6) = won12; %won with 12 numbers how many times?
case 11
won11 = won11+1;
registers{i}(7) = won11; %won with 11 numbers how many times?
end
end
end
end
registers = cat(1, registers{:});
registers(:, 1) = uint32(1):uint32(nComb)
This procudes the same result, but runs in parallel. But is this really what you want to do?
lottery = randi(25, 2400, 15, 'uint8')
A strange "lottery", which allows repeated numbers. Are you sure?
  1 个评论
Paulo Cunha
Paulo Cunha 2022-12-9
编辑:Paulo Cunha 2022-12-9
Awesome! Thank you very much!! That was great! About the line, actually I use draws from a real lottery game from my country. I included this line just to turn possible to everybody to test the code.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by