Question

Change the following code to give only the first 2 picks; or 3 picks (Matlab) %...

Change the following code to give only the first 2 picks; or 3 picks (Matlab)

% colors for ball

colors = ["red", "blue", "green", "yellow"];

% initial probabilites

% change as required

p = [0.20, 0.25, 0.35, 0.20];

% cumulative sum of p

c = cumsum(p);

% continue drawing until all are drawn

while(size(p) > 0)

% generate a random number

r = rand();

% draw ball according to random number

ball = 0;

for i = 1:size(p, 2)

if(r <= c(i))

% draw ith ball

ball = i;

break;

end

end

fprintf("%s ball is drawn.\n", colors(ball))

% delete ball

p(ball) = [];

colors(ball) = [];

% redistribute probabilities

p = p ./ sum(p);

c = cumsum(p);

end

0 0
Add a comment Improve this question Transcribed image text
Answer #1

colors = ["red", "blue", "green", "yellow"];

p = [0.20, 0.25, 0.35, 0.20];

c = cumsum(p);

while(size(p) > 0)

r = rand();

ball = 0;

for i = 1:size(p, 2)

if(r <= c(i))

% draw ith ball

ball = i;

break;

end

end

fprintf("%s ball is drawn.\n", colors(ball))

% delete ball

p(ball) = [];

colors(ball) = [];

p = p ./ sum(p);

c = cumsum(p);

end

Add a comment
Know the answer?
Add Answer to:
Change the following code to give only the first 2 picks; or 3 picks (Matlab) %...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. Consider the following Matlab code. s-0; clear s.norm for i 1:10000 r-rand(1); % generate a uniform random, numb...

    3. Consider the following Matlab code. s-0; clear s.norm for i 1:10000 r-rand(1); % generate a uniform random, number on [0,1] S-S+(3+rr) s.norm(i)-S/i end plot( 1:10000,s.norm) % make a plot of s.norma) versus i (5 pts) What will the plot look like? (10 pts) Will the function/vector s.norm(n) converge to something as n gets large? If so, what? If not, why not? Justify your answer. (5 pts) If we were to run this code multiple times - overlaying the plots...

  • Add another changeColor() method (i.e. you will now have 2 methods called "changeColor"). This one accepts...

    Add another changeColor() method (i.e. you will now have 2 methods called "changeColor"). This one accepts an int parameter and changes the color based on that int. The valid colors are "red", "yellow", "green", "blue", "magenta" and "black". In your code, map each color to an integer (e.g. in my code 3 means green.) If the number passed to the method is not valid, change the color to red. In the bounceTheBall() method, where you test for collisions with top...

  • Please solve on only PART 2 b) and c) , PART 1 is only for REFERENCE...

    Please solve on only PART 2 b) and c) , PART 1 is only for REFERENCE :) Part I: Ene concept of a percentile (equivalently, quantile) is very important in data analysis. It applies to both samples and distributions. So, let's get some wi practice with them, starting with the binomial distribution. In prelab, you learned that the function gbinom(p. size prob) gives the p-th quantile of the binomial distribution with parameters n - size and pi prob. tocus on...

  • hi which code should I use to get the following matrix and be able to change...

    hi which code should I use to get the following matrix and be able to change the number of rows. the code in the picture doesn't work for me for some reason. thank you. I use matlab program it needs to look like this and be a 10 by 10 matrix. 40 SC Matrix C sym (zeros) C(1,1) = 1; tsets the element 1,1 to be egy 42 - 44 - 45 - 46 - for n = 2:N starts...

  • (b) IULUI SAPT Two dice are rolled. Find the probabilities of the following events. 13. The...

    (b) IULUI SAPT Two dice are rolled. Find the probabilities of the following events. 13. The first die is 3 or the sum is 8. 14. The second die is 5 or the sum is 10. One card is drawn from an ordinary deck of 52 cards. Find the probabilities of drawing the following cards. 15. (a) A 9 or 10 (b) A red card or a 3 (c) A 9 or a black 10 (d) A heart or a...

  • # 3. The following code draws a sample of size $n=30$ from a chi-square distribution with...

    # 3. The following code draws a sample of size $n=30$ from a chi-square distribution with 15 degrees of freedom, and then puts $B=200$ bootstrap samples into a matrix M. After that, the 'apply' function is used to calculate the median of each bootstrap sample, giving a bootstrap sample of 200 medians. "{r} set.seed (117888) data=rchisq(30,15) M=matrix(rep(0,30*200), byrow=T, ncol=30) for (i in 1:200 M[i,]=sample(data, 30, replace=T) bootstrapmedians=apply(M,1,median) (3a) Use the 'var' command to calculate the variance of the bootstrapped medians....

  • Can you help with this C programming question. I have provided the skeleton code below along...

    Can you help with this C programming question. I have provided the skeleton code below along with the Stack/Data/Process Class for you to see/reference. Along with the Stack/Data type definition.   **SKELTON CODE** #include #include #include Stack* concat_stack(Stack *s1, Stack *s2) { //your code here return NULL; } **STACK CLASS FOR YOU TO REFERENCE** #include #include #include #include Stack* create_stack(int stack_capacity) { Stack *s = (Stack*) malloc(sizeof(Stack)); if (stack_capacity < 1) { fprintf(stderr, "Error(create_stack): invalid capacity, set to 10\n"); s->capacity =...

  • Hi, could you post solutions to the following questions. Thanks. 2. (a) Let V be a vector space on R. Give the definition of a subspace W of V 2% (b) For each of the following subsets of IR3 sta...

    Hi, could you post solutions to the following questions. Thanks. 2. (a) Let V be a vector space on R. Give the definition of a subspace W of V 2% (b) For each of the following subsets of IR3 state whether they are subepaces of R3 or not by clearly explaining your answer. 2% 2% (c) Consider the map F : R2 → R3 defined by for any z = (zi,Z2) E R2. 3% 3% 3% 3% i. Show that...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • # In this file, fill in the ... parts with lines of code. Do not # create new functions. from ran...

    # In this file, fill in the ... parts with lines of code. Do not # create new functions. from random import seed, randrange P=[" ♟♜♝♞♛♚"]; L,R,BL,TL=["▌▐▄▀"] BonR=WonR=WonB=DonR=DonB=RonB=GonR=GonB=RonG='\033[1;m\033[' WonR+='7;31;47m' # For drawing a white piece on a red background WonB+='7;30;47m' # For drawing a white piece on a black background DonR+='2;37;41m' # For drawing a dark piece on a red background DonB+='2;37;40m' # For drawing a dark piece on a black background GonR+='2;33;41m' # For drawing gold on a red...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT