Problem 87. Indexed Probability Table
This question was inspired by a Stack Overflow question forwarded to me by Matt Simoneau.
Given a vector x, make an indexed probability vector p that shows the likelihood of each unique element of x occurring. You can assume that x will always be composed of integers greater than 0.
For example, if x is [4 4 4 4] then the number 4 is 100% likely to occur and no other number has any likelihood of occurring. By convention, p has as many elements as the highest number in x. This makes the indexing straightforward. So in this case, p = [0 0 0 1]. That is, p(4) = 1, which is to say that 4 occurs with 100% probability, as noted above. Note that p has no dependence on the order of the elements in x.
If
x = [1 2 1 2 1 2 1 2 1 2]
then
p = [0.5 0.5].
And if
x = [5 5 2 8]
then
p = [0 0.25 0 0 0.5 0 0 0.25]
Solution Stats
Problem Comments
-
1 Comment
Gareth Lee
on 2 Dec 2014
Is it not permitted by using tabulate function to solve it?
Solution Comments
Show commentsProblem Recent Solvers2437
Suggested Problems
-
13185 Solvers
-
Remove any row in which a NaN appears
8549 Solvers
-
Number of 1s in the Binary Representation of a Number
442 Solvers
-
Find the largest value in the 3D matrix
1495 Solvers
-
Natural numbers in string form
1375 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!