Help with simple probability calculation (loop?)

조회 수: 1 (최근 30일)
Cary
Cary 2015년 11월 25일
댓글: Star Strider 2015년 11월 30일
I have a 2-column matrix that looks like this:
30 1
30 1
30 1
30 1
30 1
29 0
29 1
29 1
28 1
28 1
28 0
28 0
"1" indicates an occurrence and "0" does not. I need to compute a probability for each unique number in column 1. So the answers should be:
30 = 1
29 = .6667
28 = .50
Now, I have the "count_unique" function from the file exchange that counts the unique numbers and identifies how many occurrences there are for each number. Using this, I have tried to write a for loop that computes this problem, but I am stuck in the mud. I'm grateful for any assistance. Happy Thanksgiving.

채택된 답변

Star Strider
Star Strider 2015년 11월 25일
I don’t know how robust this is, but it works here:
M = [30 1
30 1
30 1
30 1
30 1
29 0
29 1
29 1
28 1
28 1
28 0
28 0];
raw_freq = accumarray(M(:,1), 1);
adj_freq = accumarray(M(M(:,2)>0), 1);
Prob = adj_freq./raw_freq;
Mu = unique(M(:,1));
Result = flipud([Mu Prob(~isnan(Prob))])
Result =
30 1
29 0.66667
28 0.5
  댓글 수: 2
Cary
Cary 2015년 11월 30일
Thanks as always Star! Happy Holidays.
Star Strider
Star Strider 2015년 11월 30일
And as always, my pleasure!
You, too!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by