MATLAB rand(m,n)

74 Views Asked by At

I want reconfirm what I believe is the answer. So this is the question: "Make a vector in MATLAB with $n = 10$ random elements, and then find the percentage of those elements that are less than $\frac{1}{2}$."

Here is my code:

n = 10;
a = rand(1, n);
b = a<1/2; 
disp(sum(b)*100/n)

Every time I run the script, I would get a different answer, i.e. $40$ then $70$ then $40$ again, etc. Now I believe the code is right but the outputs are different because it is taking the percentage of $10$ random elements hence the answer will continue outputting different answers. But the solution, states that it was $30$% which confused me and don't know whether that is the definite solution or it is one of many possible solutions. Thanks !!!!

1

There are 1 best solutions below

0
On BEST ANSWER

The code is correct.

Since there are only 10 numbers you will always get one of 0%, 10%, 20%, ...., 90%, 100%.

The answer "30%" is only an example of a possible outcome.

Since the output is based on data randomly generated it is normal it's not exactly equal to what you get.