find probability of number in a matrix

3 vues (au cours des 30 derniers jours)
elisa ewin
elisa ewin le 10 Nov 2016
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

Réponse acceptée

Alexandra Harkai
Alexandra Harkai le 10 Nov 2016
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

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by