Examples
Suppose we wish to flip a coin N times and count how many times HEADS was flipped. An algorithm that solves this problem, could be:
Get N from somewhere (parameter or user input or ???) declare a counter variable with two values (1 for heads and 2 for tails) for each integer 1 to N flip the coin (use
randperm
) increment the first or second element based on flip result display the HEADS and TAILS valuesWe need a variable to keep count, but we also need a variable N to tell us how many times to repeat the coin flip. The wordfor
is a Matlab command that sets up an iteration through each value in a vector of values. It is another type of control statement available in Matlab.