How to write a loop for vectors?

6 views (last 30 days)
Nick Haufler
Nick Haufler on 2 Oct 2015
Edited: Kirby Fears on 2 Oct 2015
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.

Accepted Answer

Kirby Fears
Kirby Fears on 2 Oct 2015
Edited: Kirby Fears on 2 Oct 2015
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.
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
% Find matches and count them, from 5oK down to 2oK
for k=5:-1:2,

More Answers (1)

Manolis Michailidis
Manolis Michailidis on 2 Oct 2015
Hello, the main issue is that in your loop you should use cell because for every iteration you have different dice results and the new overwrite the previous. Here is what i done see if it suits your demands. Also the ''counts'' variable does not seems ok to me so i made some modification in your code. As for the display part you can modify it for your needs. Note it maybe much simplier ways for counter elements ( ) but i don't have much time and please copy the 'cell2mat.m'. function from below to your matlab path, it trasforms cell array into matrix.
numRolls=input('Please enter how many rolls you would like to simulate:');
%counts =zeros([1,5]); % list of how many times each number came up
for r = 1 : numRolls
rolls{r} = randi([1 6],[1 5]);
rolls{r} = sort(rolls{r});
for kk=1:size(c1,2)
s3(:,kk)=sum(c1(:,kk) == 3); % how many times '3' appears
s4(:,kk)=sum(c1(:,kk) == 4); % how many times '4' appears
s5(:,kk)=sum(c1(:,kk) == 5); % how many times '5' appears
%if any(counts == 3)
%fprintf('The role is a three of a kind')
%elseif any(counts == 4)
%fprintf('The role is a four of a kind')
%elseif any(counts == 5)
%fprintf('The role is a five of a kind')
% Now normalize to get fraction of time each number came up.
%counts = (counts / numRolls)*100 % Multiply by 100 if you want percentage instead of a fraction.
function [Y]=cell2array(X)
lns = cellfun(@length,X);
mt = zeros(numel(X),max(lns));
for ii = 1:numel(X)
mt(ii,1:lns(ii)) = X{ii};

Community Treasure Hunt

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

Start Hunting!