필터 지우기
필터 지우기

find probability of number in a matrix

조회 수: 1 (최근 30일)
elisa ewin
elisa ewin 2016년 11월 10일
답변: Alexandra Harkai 2016년 11월 10일
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