function prob = ex4(n)
prob = zeros(1,12);
TotalNRolls = 0;
while TotalNRolls <= n
Conceptual question: what is n? Is it the number of dice you roll (in which case n = 3 would require rolling the pair of dice twice or require you to stop after your first roll lest you go over the number of dice you're allowed to roll?) Or is it the number of times you roll the pair of dice (in which case n = 3 would require rolling a total of six dice?)
die(1) = randi(6);
die(2) = randi(6);
Rather than calling randi twice, consider calling it once to get a 1-by-2 (or a 2-by-1) vector of random numbers. Take a look at the randi documentation, it contains examples showing how to generate more than one number at a time.
It also seems a bit odd to me to store the results of rolling a pair of dice (plural) in a variable named die (singular.)
As Rik suggested, don't use sum as a variable name.
TotalNRolls = TotalNRolls + 2;
if sum == 2
prob(2) = TotalNRolls/n;
elseif sum == 3
prob(3) = TotalNRolls/n;
I wouldn't do the division inside the loop. I would just count how many times each result occurred inside the loop and divide by the number of rolls afterwards.
Also note that the lines of code in your if / elseif / else block look awfully similar. Do you see a pattern in the sum of the two rolled dice and which element of your probability vector needs to be updated? That whole if / elseif / else block can be collapsed to one line.