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:
Array referencing the Matlab way

Subject: Array referencing the Matlab way

From: Jeff

Date: 2 Feb, 2010 07:51:06

Message: 1 of 6

It seems there should be a more elegant - read "show-offy" - way to write the following in Matlab. It's supposed to simulate probabilities of rolling a fair, six-sided die 100 times.

rolls=rand(100,1);
for i=1:max(size(rolls))
    if 0*1/6<=rolls(i) && rolls(i)<=1*1/6, rolls(i)=1, end
    if 1*1/6<=rolls(i) && rolls(i)<=2*1/6, rolls(i)=2, end
    if 2*1/6<=rolls(i) && rolls(i)<=3*1/6, rolls(i)=3, end
    if 3*1/6<=rolls(i) && rolls(i)<=4*1/6, rolls(i)=4, end
    if 4*1/6<=rolls(i) && rolls(i)<=5*1/6, rolls(i)=5, end
    if 5*1/6<=rolls(i) && rolls(i)<=6*1/6, rolls(i)=6, end
end

Any help? If so, thanks,
-Jeff

Subject: Array referencing the Matlab way

From: us

Date: 2 Feb, 2010 08:06:03

Message: 2 of 6

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hk8lha$haq$1@fred.mathworks.com>...
> It seems there should be a more elegant - read "show-offy" - way to write the following in Matlab. It's supposed to simulate probabilities of rolling a fair, six-sided die 100 times.
>
> rolls=rand(100,1);
> for i=1:max(size(rolls))
> if 0*1/6<=rolls(i) && rolls(i)<=1*1/6, rolls(i)=1, end
> if 1*1/6<=rolls(i) && rolls(i)<=2*1/6, rolls(i)=2, end
> if 2*1/6<=rolls(i) && rolls(i)<=3*1/6, rolls(i)=3, end
> if 3*1/6<=rolls(i) && rolls(i)<=4*1/6, rolls(i)=4, end
> if 4*1/6<=rolls(i) && rolls(i)<=5*1/6, rolls(i)=5, end
> if 5*1/6<=rolls(i) && rolls(i)<=6*1/6, rolls(i)=6, end
> end
>
> Any help? If so, thanks,
> -Jeff

one of the many solutions

     r=ceil(6*rand(1,10))
% r = 1 6 3 1 3 5 5 4 5 6 % <- your series will look different, of course...

us

Subject: Array referencing the Matlab way

From: Jeff

Date: 2 Feb, 2010 08:53:05

Message: 3 of 6

"us " <us@neurol.unizh.ch> wrote in message <hk8mdb$qn3$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hk8lha$haq$1@fred.mathworks.com>...
> > It seems there should be a more elegant - read "show-offy" - way to write the following in Matlab. It's supposed to simulate probabilities of rolling a fair, six-sided die 100 times.
> >
> > rolls=rand(100,1);
> > for i=1:max(size(rolls))
> > if 0*1/6<=rolls(i) && rolls(i)<=1*1/6, rolls(i)=1, end
> > if 1*1/6<=rolls(i) && rolls(i)<=2*1/6, rolls(i)=2, end
> > if 2*1/6<=rolls(i) && rolls(i)<=3*1/6, rolls(i)=3, end
> > if 3*1/6<=rolls(i) && rolls(i)<=4*1/6, rolls(i)=4, end
> > if 4*1/6<=rolls(i) && rolls(i)<=5*1/6, rolls(i)=5, end
> > if 5*1/6<=rolls(i) && rolls(i)<=6*1/6, rolls(i)=6, end
> > end
> >
> > Any help? If so, thanks,
> > -Jeff
>
> one of the many solutions
>
> r=ceil(6*rand(1,10))
> % r = 1 6 3 1 3 5 5 4 5 6 % <- your series will look different, of course...
>
> us

i made it too complicated. I was thinking of something fancy, like:

rolls=rand(10,1);
rolls=(rolls<=1/6)*1 || (rolls<=2/6)*2 ...

So then here's a harder question (question 2 of my homework). Roll 2 loaded dice with the given probabilities, and program a monte carlo simulation of 300 runs:

Die 1:
1 - .1
2 - .1
3 - .2
4 - .3
5 - .2
6 - .1

Die 2:
1 - .3
2 - .1
3 - .2
4 - .1
5 - .05
6 - .25

I've solved this using good old brute force, but I want to get the hang of Matlab's cool array indexing features. Here's my beginner's solution which my dog could program:

Ndice=2;
for j=1:300
    % Rolls Ndice dice.
    rolls=rand(Ndice,1);
    
    % Update counts for die 1.
    if .00<=rolls(1) && rolls(1)<.10, counter(1,1)=counter(1,1)+1;
    elseif .10<=rolls(1) && rolls(1)<.20, counter(1,2)=counter(1,2)+1;
    elseif .20<=rolls(1) && rolls(1)<.40, counter(1,3)=counter(1,3)+1;
    elseif .40<=rolls(1) && rolls(1)<.70, counter(1,4)=counter(1,4)+1;
    elseif .70<=rolls(1) && rolls(1)<.90, counter(1,5)=counter(1,5)+1;
    elseif .90<=rolls(1) && rolls(1)<1, counter(1,6)=counter(1,6)+1;
    end
    
    % Update counts for die 2.
    if .00<=rolls(2) && rolls(2)<.30, counter(2,1)=counter(2,1)+1;
    elseif .30<=rolls(2) && rolls(2)<.40, counter(2,2)=counter(2,2)+1;
    elseif .40<=rolls(2) && rolls(2)<.60, counter(2,3)=counter(2,3)+1;
    elseif .60<=rolls(2) && rolls(2)<.70, counter(2,4)=counter(2,4)+1;
    elseif .70<=rolls(2) && rolls(2)<.75, counter(2,5)=counter(2,5)+1;
    elseif .75<=rolls(2) && rolls(2)<1, counter(2,6)=counter(2,6)+1;
    end
    
end

Subject: Array referencing the Matlab way

From: us

Date: 2 Feb, 2010 09:10:20

Message: 4 of 6

"Jeff "
> I've solved this using good old brute force, but I want to get the hang of Matlab's cool array indexing features. Here's my beginner's solution which my dog could program:
> if .00<=rolls(1) && rolls(1)<.10, counter(1,1)=counter(1,1)+1;
> elseif .10<=rolls(1) && rolls(1)<.20, counter(1,2)=counter(1,2)+1;
> elseif .20<=rolls(1) && rolls(1)<.40, counter(1,3)=counter(1,3)+1;
> elseif .40<=rolls(1) && rolls(1)<.70, counter(1,4)=counter(1,4)+1;
> elseif .70<=rolls(1) && rolls(1)<.90, counter(1,5)=counter(1,5)+1;
> elseif .90<=rolls(1) && rolls(1)<1, counter(1,6)=counter(1,6)+1;
> end

one of the many solutions

     nt=300;
     r=rand(1,nt);
     p=[0,.1,.2,.4,.7,.9,inf];
     n=histc(r,p);
     n=n(1:6);
     disp([p(1:6);n].');
%{
       0 32
     0.1 33
     0.2 58
     0.4 91
     0.7 60
     0.9 26
%}

us

Subject: Array referencing the Matlab way

From: Steven Lord

Date: 2 Feb, 2010 14:53:26

Message: 5 of 6


"us " <us@neurol.unizh.ch> wrote in message
news:hk8mdb$qn3$1@fred.mathworks.com...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> <hk8lha$haq$1@fred.mathworks.com>...
>> It seems there should be a more elegant - read "show-offy" - way to write
>> the following in Matlab. It's supposed to simulate probabilities of
>> rolling a fair, six-sided die 100 times.
>>
>> rolls=rand(100,1);
>> for i=1:max(size(rolls))
>> if 0*1/6<=rolls(i) && rolls(i)<=1*1/6, rolls(i)=1, end
>> if 1*1/6<=rolls(i) && rolls(i)<=2*1/6, rolls(i)=2, end
>> if 2*1/6<=rolls(i) && rolls(i)<=3*1/6, rolls(i)=3, end
>> if 3*1/6<=rolls(i) && rolls(i)<=4*1/6, rolls(i)=4, end
>> if 4*1/6<=rolls(i) && rolls(i)<=5*1/6, rolls(i)=5, end
>> if 5*1/6<=rolls(i) && rolls(i)<=6*1/6, rolls(i)=6, end
>> end
>>
>> Any help? If so, thanks,
>> -Jeff
>
> one of the many solutions
>
> r=ceil(6*rand(1,10))
> % r = 1 6 3 1 3 5 5 4 5 6 % <- your series will look different, of
> course...

Two other solutions; the first is the one I'd probably use:

r = randi([1 6], 100, 1);

rolls = rand(100, 1);
[rollcounts, therolls] = histc(rolls, linspace(0, 1, 7));

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Array referencing the Matlab way

From: Jeff

Date: 2 Feb, 2010 17:12:20

Message: 6 of 6

Steven and us:

This is the stats part of my course (Applied Industrial Math). So I read about the histc commands you guys used. The point is that these are very useful solutions for me. Thanks.

-Jeff

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