필터 지우기
필터 지우기

Calculate probability of number appearing in a column

조회 수: 2 (최근 30일)
Emma Kuttler
Emma Kuttler 2019년 11월 21일
답변: Walter Roberson 2019년 11월 23일
I have a 1000x296 matrix called Values. The rows represent trials of a simulation, and the rows represent the rank (1-296) of a particular element.
I want to create a matrix that returns the probability of a column having a particular value. I assume i will need a set of nested for loops to count the number of times each value appears in each column, and then divide by the number of rows (1000).
E.g.
The value in cell (1,1) will have the probability of a particular row of column 1 having value 1
The value in cell (1,15) will have the probability of a particular row of column 1 having value 15
The value in cell (200, 155) will have the probability of a particular row of column 200 having value 155.
Thanks!
  댓글 수: 3
Emma Kuttler
Emma Kuttler 2019년 11월 22일
I'm not looking for the probability distribution, I'm looking for the simple probability of a value appearing in each column.
So more like counting (for example) the number of times the value 1 appears in column 1, dividing by the number of rows, and returning that value in position (1,1). Then counting the number of times 2 appears in column 1, dividing by the number of rows, and returning that in (2,1). Then repeating this for all the columns.
Walter Roberson
Walter Roberson 2019년 11월 22일
Neither accumarray nor histcounts2 calculate probability distributions. In its simplest form, accumarray just counts.

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2019년 11월 21일
Just loop over the array getting the histogram in each column.
Something like (untested)
numBins = 25; % Whatever you want.
edges = linspace(0, max(Values(:), numBins);
[rows, columns] = size(Values);
allCounts = zeros(numBins, columns);
for col = 1 : columns
thisColumn = Values(:, col);
counts = histcounts(thisColumns, edges);
allCounts(:, col) = allCounts(:, col) + counts;
end
% Normalize
allCounts = allCounts / sum(allCounts(:));
bar3(allCounts); % Display

Walter Roberson
Walter Roberson 2019년 11월 23일
[rowidx, colidx] = ndgrid(1:size(Values,1), 1:size(Values,2));
probs = accumarray([colidx(:), Values(:)],1) ./ size(Values,1);;

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by