Calculate probability of number appearing in a column

5 次查看(过去 30 天)
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