Filter löschen
Filter löschen

find probability of number in a matrix

1 Ansicht (letzte 30 Tage)
elisa ewin
elisa ewin am 10 Nov. 2016
Beantwortet: Alexandra Harkai am 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

Akzeptierte Antwort

Alexandra Harkai
Alexandra Harkai am 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

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by