MATLAB: How to write a loop for vectors

for loopMATLABsimulationsimulink

For the attached document can someone tell me if my script file is correct. Im confused on how to get the output to determine if its a pair or 3,4,5 of a kind. I'll attach the script and if someone could maybe run it, and see whats going on with it that would help. Ive went to the tutoring lab, but no one could help. I dont know if this question is just tough or what.

Best Answer

You can draw your 5 random dice values across all rolls at once (no need to loop). You can then calculate the 5 of a kind, then 4 of a kind, then 3 of a kind, then 2 of a kind. Keep track of the higher hands already counted so you don't double count. This code could be further refined to count how many times you get "two pair" but that wasn't requested in the pdf.
rng('shuffle');
numRolls=input('Please enter how many rolls you would like to simulate:');
counts = zeros(1,4); % Store counts of 2oK, 3oK, 4oK, 5oK
numDice=5; % Number of dice per roll
% Draw 5 random dice per roll
rolls = randi([1, 6],[numRolls, 5]);
% Sort dice per roll
rolls = sort(rolls,2);
% Keep track of better matches already counted
logIdx=false(size(rolls,1),1);
% Find matches and count them, from 5oK down to 2oK
for k=5:-1:2,
kIdx=any(rolls(:,1:1+(5-k))==rolls(:,end-(5-k):end),2);
counts(k-1)=sum(kIdx&~logIdx);
logIdx=logIdx|kIdx;
end,
percentMatches=counts*100/numRolls;