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

采纳的回答

Alexandra Harkai
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 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by