Compute probability of each element in each column of a m x n matrix.

7 次查看(过去 30 天)
Hello Friends,
I have a m x n matrix. I want to compute the probability of each element occurring in a column. I want to do it for each column.
For instance, suppose we have a 3 x 4 matrix:
A = [1 1 3 2; 2 3 0 2; 3 1 0 2];
Then, I would like to have the following matrix of probabilities: _P(A) = [ 1/3 2/3 1/3 1; 1/3 1/3 2/3 1; 1/3 2/3 2/3 1];_
Here, entries in P(A) are the probabilities of each element within the column.
For the 1st column: p(1) = p(2) = p(3) = 1/3
For the 2nd column: p(1) = 2/3, p(3) = 1/3
For the 3rd column: p(3) = 1/3, p(0) = 2/3
For the 4th column: p(2) = 3/3 = 1.
Please advise.

采纳的回答

hello_world
hello_world 2014-11-25
I found the solution of my own problem posted above. Though, thanks for your kind help. I will still appreciate different ways to solve it specially with hist(), and histc(). Here is my solution to this problem:
[r, c] = size(A);
y = zeros(r,c);
p = zeros(r,c);
for i = 1:c %Looping through the column.
for j = 1:r %Looping through the row in that that column.
y(j,i) = sum(A(:,[i]) == A(j,i)); %Compare each column of the matrix with the entries in that column.
p(j,i) = y(j,i)/r; %Probability of each entry of a column within that column.
end
end

更多回答(2 个)

Sukumar GV
Sukumar GV 2018-7-29
You can use the following.
Suppose Matrix is X
[a, b] = hist (X, unique(X))
a/size(X,1)
each row represents item and each column represents the dimension

Yu Jiang
Yu Jiang 2014-11-24
编辑:Yu Jiang 2014-11-24
One possible but most likely not optimal way is to use the function "find" to count the number of occurrence of a particular number. For example,
[r,c] = size(A); % get the column of A
nr = 0:3; %range of the numbers
P = zeros(r,c); % create a matrix to store the probabilities
for j = 1:c % check each row
for i = 1:length(nr) % check each particular number
P(i,j) = length(find(A(:,j) == nr(i)))/r; % count the occurrence
end
end
  3 个评论
Hugo
Hugo 2014-11-24
The range of the numbers refers to the minimum and the maximum number in your matrix. You can just replace the line nr=0:3; with nr=min(A(:)):max(A(:));
This will only work, of course, if A contains only integers. If it doesn't, things get a little more complicated. Hope this helps.
hello_world
hello_world 2014-11-24
编辑:hello_world 2014-11-24
Thanks for help, though entries in my matrix are not integers. They are real number.
By making your suggested improvement, it gives me a 6 x 5 matrix of probabilities for a 3 x 5 size of matrix A. How is it possible? Should not I get a 3 x 5 size probability matrix instead?

请先登录,再进行评论。

类别

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