Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
More Matlab array indexing questions

Subject: More Matlab array indexing questions

From: Jeff

Date: 4 Feb, 2010 19:50:21

Message: 1 of 7

How would I get *this* to work? Am I stuck writing a loop from 1:Nrolls to test each roll?

I'm rolling two unfair dice 300 times, using rand. Then I want to assign the correct value of the dice for each rolll. For example, on die 1 there is a 10% chance of rolling a 1 or 2, a 20% chance of rolling a 3...

Ndice=2;
Nrolls=300;
    rolls=rand(Ndice,Nrolls);
    
    % Set value for die 1.
    if .00<=rolls(1,:) & rolls(1,:)<.10, rolls(1,:)=1;
    elseif .10<=rolls(1,:) & rolls(1,:)<.20, rolls(1,:)=2;
    elseif .20<=rolls(1,:) & rolls(1,:)<.40, rolls(1,:)=3;
    elseif .40<=rolls(1,:) & rolls(1,:)<.70, rolls(1,:)=4;
    elseif .70<=rolls(1,:) & rolls(1,:)<.90, rolls(1,:)=5;
    elseif .90<=rolls(1,:) & rolls(1,:)<1, rolls(1,:)=6;
    else error('Bad programmer!')
    end
    
    % Set value for die 2.
    if .00<=rolls(2,:) & rolls(2,:)<.30, rolls(2,:)=1;
    elseif .30<=rolls(2,:) & rolls(2,:)<.40, rolls(2,:)=2;
    elseif .40<=rolls(2,:) & rolls(2,:)<.60, rolls(2,:)=3;
    elseif .60<=rolls(2,:) & rolls(2,:)<.70, rolls(2,:)=4;
    elseif .70<=rolls(2,:) & rolls(2,:)<.75, rolls(2,:)=5;
    elseif .75<=rolls(2,:) & rolls(2,:)<1, rolls(2,:)=6;
    else error('Bad programmer!')
    end

-Jeff

Subject: More Matlab array indexing questions

From: Jos (10584)

Date: 4 Feb, 2010 20:03:04

Message: 2 of 7

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hkf8dt$imc$1@fred.mathworks.com>...
> How would I get *this* to work? Am I stuck writing a loop from 1:Nrolls to test each roll?
>
> I'm rolling two unfair dice 300 times, using rand. Then I want to assign the correct value of the dice for each rolll. For example, on die 1 there is a 10% chance of rolling a 1 or 2, a 20% chance of rolling a 3...
>
> Ndice=2;
> Nrolls=300;
> rolls=rand(Ndice,Nrolls);
>
> % Set value for die 1.
> if .00<=rolls(1,:) & rolls(1,:)<.10, rolls(1,:)=1;
> elseif .10<=rolls(1,:) & rolls(1,:)<.20, rolls(1,:)=2;
> elseif .20<=rolls(1,:) & rolls(1,:)<.40, rolls(1,:)=3;
> elseif .40<=rolls(1,:) & rolls(1,:)<.70, rolls(1,:)=4;
> elseif .70<=rolls(1,:) & rolls(1,:)<.90, rolls(1,:)=5;
> elseif .90<=rolls(1,:) & rolls(1,:)<1, rolls(1,:)=6;
> else error('Bad programmer!')
> end
>
> % Set value for die 2.
> if .00<=rolls(2,:) & rolls(2,:)<.30, rolls(2,:)=1;
> elseif .30<=rolls(2,:) & rolls(2,:)<.40, rolls(2,:)=2;
> elseif .40<=rolls(2,:) & rolls(2,:)<.60, rolls(2,:)=3;
> elseif .60<=rolls(2,:) & rolls(2,:)<.70, rolls(2,:)=4;
> elseif .70<=rolls(2,:) & rolls(2,:)<.75, rolls(2,:)=5;
> elseif .75<=rolls(2,:) & rolls(2,:)<1, rolls(2,:)=6;
> else error('Bad programmer!')
> end
>
> -Jeff

Take a look at RANDP

% data
  Nrolls = 300 ;
  Ndice = 2 ;
  UnfairDistribution = [3 1 2 1 .5 2.5] ;
% engine
  % Select random numbers between 1 and 6
  Rolls = randp(UnfairDistribution,[Nrolls Ndice]) ;
% result
  % this should be similar to the unfair distribution
  histc(Rolls,1:6) ./ Nrolls

RANDP can be found here:
http://www.mathworks.com/matlabcentral/fileexchange/8891-randp

hth
Jos

Subject: More Matlab array indexing questions

From: Walter Roberson

Date: 4 Feb, 2010 20:04:29

Message: 3 of 7

Jeff wrote:
> How would I get *this* to work? Am I stuck writing a loop from 1:Nrolls
> to test each roll?
>
> I'm rolling two unfair dice 300 times, using rand. Then I want to assign
> the correct value of the dice for each rolll. For example, on die 1
> there is a 10% chance of rolling a 1 or 2, a 20% chance of rolling a 3...
>
> Ndice=2;
> Nrolls=300; rolls=rand(Ndice,Nrolls);
> % Set value for die 1.
> if .00<=rolls(1,:) & rolls(1,:)<.10, rolls(1,:)=1;
> elseif .10<=rolls(1,:) & rolls(1,:)<.20, rolls(1,:)=2;
> elseif .20<=rolls(1,:) & rolls(1,:)<.40, rolls(1,:)=3;
> elseif .40<=rolls(1,:) & rolls(1,:)<.70, rolls(1,:)=4;
> elseif .70<=rolls(1,:) & rolls(1,:)<.90, rolls(1,:)=5;
> elseif .90<=rolls(1,:) & rolls(1,:)<1, rolls(1,:)=6;


> else error('Bad programmer!')
> end
> % Set value for die 2.
> if .00<=rolls(2,:) & rolls(2,:)<.30, rolls(2,:)=1;
> elseif .30<=rolls(2,:) & rolls(2,:)<.40, rolls(2,:)=2;
> elseif .40<=rolls(2,:) & rolls(2,:)<.60, rolls(2,:)=3;
> elseif .60<=rolls(2,:) & rolls(2,:)<.70, rolls(2,:)=4;
> elseif .70<=rolls(2,:) & rolls(2,:)<.75, rolls(2,:)=5;
> elseif .75<=rolls(2,:) & rolls(2,:)<1, rolls(2,:)=6;
> else error('Bad programmer!')
> end
>
> -Jeff

One method:

bounds1 = [0.1 0.2 0.4 0.7 0.9];
bounds2 = [.3 .4 .6 .7 .75];
dierolls(1,:) = 1 + sum(repmat(rolls(1,:).',1,length(bounds1)) ...
 >= repmat(bounds1,nrolls,1),2);
dierolls(2,:) = 1 + sum(repmat(rolls(2,:).',1,length(bounds2)) ...
 >= repmat(bounds2,nrolls,1),2);

Subject: More Matlab array indexing questions

From: Jeff

Date: 4 Feb, 2010 20:34:04

Message: 4 of 7

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hkf95o$6k8$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hkf8dt$imc$1@fred.mathworks.com>...
> > How would I get *this* to work? Am I stuck writing a loop from 1:Nrolls to test each roll?
> >
> > I'm rolling two unfair dice 300 times, using rand. Then I want to assign the correct value of the dice for each rolll. For example, on die 1 there is a 10% chance of rolling a 1 or 2, a 20% chance of rolling a 3...
> >
> > Ndice=2;
> > Nrolls=300;
> > rolls=rand(Ndice,Nrolls);
> >
> > % Set value for die 1.
> > if .00<=rolls(1,:) & rolls(1,:)<.10, rolls(1,:)=1;
> > elseif .10<=rolls(1,:) & rolls(1,:)<.20, rolls(1,:)=2;
> > elseif .20<=rolls(1,:) & rolls(1,:)<.40, rolls(1,:)=3;
> > elseif .40<=rolls(1,:) & rolls(1,:)<.70, rolls(1,:)=4;
> > elseif .70<=rolls(1,:) & rolls(1,:)<.90, rolls(1,:)=5;
> > elseif .90<=rolls(1,:) & rolls(1,:)<1, rolls(1,:)=6;
> > else error('Bad programmer!')
> > end
> >
> > % Set value for die 2.
> > if .00<=rolls(2,:) & rolls(2,:)<.30, rolls(2,:)=1;
> > elseif .30<=rolls(2,:) & rolls(2,:)<.40, rolls(2,:)=2;
> > elseif .40<=rolls(2,:) & rolls(2,:)<.60, rolls(2,:)=3;
> > elseif .60<=rolls(2,:) & rolls(2,:)<.70, rolls(2,:)=4;
> > elseif .70<=rolls(2,:) & rolls(2,:)<.75, rolls(2,:)=5;
> > elseif .75<=rolls(2,:) & rolls(2,:)<1, rolls(2,:)=6;
> > else error('Bad programmer!')
> > end
> >
> > -Jeff
>
> Take a look at RANDP
>
> % data
> Nrolls = 300 ;
> Ndice = 2 ;
> UnfairDistribution = [3 1 2 1 .5 2.5] ;
> % engine
> % Select random numbers between 1 and 6
> Rolls = randp(UnfairDistribution,[Nrolls Ndice]) ;
> % result
> % this should be similar to the unfair distribution
> histc(Rolls,1:6) ./ Nrolls
>
> RANDP can be found here:
> http://www.mathworks.com/matlabcentral/fileexchange/8891-randp
>
> hth
> Jos

Nice easy solution. But my Matlab doesn't seem to have randp. It may not be part of the student version.

Subject: More Matlab array indexing questions

From: Jeff

Date: 5 Feb, 2010 03:49:03

Message: 5 of 7

Walter Roberson <roberson@hushmail.com> wrote in message <hkf9n9$qej$1@canopus.cc.umanitoba.ca>...
> Jeff wrote:
> > How would I get *this* to work? Am I stuck writing a loop from 1:Nrolls
> > to test each roll?
> >
> > I'm rolling two unfair dice 300 times, using rand. Then I want to assign
> > the correct value of the dice for each rolll. For example, on die 1
> > there is a 10% chance of rolling a 1 or 2, a 20% chance of rolling a 3...
> >
> > Ndice=2;
> > Nrolls=300; rolls=rand(Ndice,Nrolls);
> > % Set value for die 1.
> > if .00<=rolls(1,:) & rolls(1,:)<.10, rolls(1,:)=1;
> > elseif .10<=rolls(1,:) & rolls(1,:)<.20, rolls(1,:)=2;
> > elseif .20<=rolls(1,:) & rolls(1,:)<.40, rolls(1,:)=3;
> > elseif .40<=rolls(1,:) & rolls(1,:)<.70, rolls(1,:)=4;
> > elseif .70<=rolls(1,:) & rolls(1,:)<.90, rolls(1,:)=5;
> > elseif .90<=rolls(1,:) & rolls(1,:)<1, rolls(1,:)=6;
>
>
> > else error('Bad programmer!')
> > end
> > % Set value for die 2.
> > if .00<=rolls(2,:) & rolls(2,:)<.30, rolls(2,:)=1;
> > elseif .30<=rolls(2,:) & rolls(2,:)<.40, rolls(2,:)=2;
> > elseif .40<=rolls(2,:) & rolls(2,:)<.60, rolls(2,:)=3;
> > elseif .60<=rolls(2,:) & rolls(2,:)<.70, rolls(2,:)=4;
> > elseif .70<=rolls(2,:) & rolls(2,:)<.75, rolls(2,:)=5;
> > elseif .75<=rolls(2,:) & rolls(2,:)<1, rolls(2,:)=6;
> > else error('Bad programmer!')
> > end
> >
> > -Jeff
>
> One method:
>
> bounds1 = [0.1 0.2 0.4 0.7 0.9];
> bounds2 = [.3 .4 .6 .7 .75];
> dierolls(1,:) = 1 + sum(repmat(rolls(1,:).',1,length(bounds1)) ...
> >= repmat(bounds1,nrolls,1),2);
> dierolls(2,:) = 1 + sum(repmat(rolls(2,:).',1,length(bounds2)) ...
> >= repmat(bounds2,nrolls,1),2);

Wow. Who would have thought of that? Works great.

Subject: More Matlab array indexing questions

From: Jos (10584)

Date: 5 Feb, 2010 12:14:05

Message: 6 of 7

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> >
> > RANDP can be found here:
> > http://www.mathworks.com/matlabcentral/fileexchange/8891-randp

> Nice easy solution. But my Matlab doesn't seem to have randp. It may not be part of the student version.

It is not even part of the standard matlab package! I assume you are not familiar with the Matlab File Exchange? It is a repository for functions ML users wishes to share with the world. It's free and has a lot of worthwhile submissions.

I have submitted RANDP to it:
http://www.mathworks.com/matlabcentral/fileexchange/8891-randp

You can download it and put the m-file on your matlab path. For instance, you can create a folder named "fexfiles", add it to your standard path.

hth
Jos

Subject: More Matlab array indexing questions

From: Jeff

Date: 5 Feb, 2010 16:52:04

Message: 7 of 7

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hkh22d$rh6$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> > >
> > > RANDP can be found here:
> > > http://www.mathworks.com/matlabcentral/fileexchange/8891-randp
>
> > Nice easy solution. But my Matlab doesn't seem to have randp. It may not be part of the student version.
>
> It is not even part of the standard matlab package! I assume you are not familiar with the Matlab File Exchange? It is a repository for functions ML users wishes to share with the world. It's free and has a lot of worthwhile submissions.
>
> I have submitted RANDP to it:
> http://www.mathworks.com/matlabcentral/fileexchange/8891-randp
>
> You can download it and put the m-file on your matlab path. For instance, you can create a folder named "fexfiles", add it to your standard path.
>
> hth
> Jos

Sweet! Thanks for the clarification (I guess if I hadn't been so stressed about my homework, I could've just read the page at your link the first time :D).

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us