find probability of number in a matrix
2 ビュー (過去 30 日間)
古いコメントを表示
Hi! I have a matrix
A=[2 1 4 0 0; 3 2 1 3 2; 0 2 0 2 1]
I want to find the probability of numbers 1,2,3,4 for every columns. When in the matrix there is a 0, if like there isn't nothing in its position.
for example: probability of 2 in the first column is 1/2; probability of 3 in the first column is 1/2; probability of 2 in the second column is 2/3 and so on
I want realize a matrix like this:
B = [1 0 1/3 1/2 0 1/2; 2 1/2 2/3 0 1/2 1/2; 3 1/2 0 0 1/2 0; 4 0 0 1/2 0 0]
first column indicates the values I want to find the probabilities and the other columns are the probabilities of this values in every column.
Thanks for help
0 件のコメント
採用された回答
Alexandra Harkai
2016 年 11 月 10 日
You can sum all elements in each column that matches, and divide by the number of nonzero elements:
v = (1:4)'; % your numbers
B = cell2mat(arrayfun(@(x) sum(A==x,1)./sum(A~=0,1), v, 'UniformOutput', false)); % actual calc performed on each element of array v
B = [v, B]; % appending the first column of 1..4 numbers
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!