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 个评论
Walter Roberson
2019-11-22
Neither accumarray nor histcounts2 calculate probability distributions. In its simplest form, accumarray just counts.
回答(2 个)
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
0 个评论
Walter Roberson
2019-11-23
[rowidx, colidx] = ndgrid(1:size(Values,1), 1:size(Values,2));
probs = accumarray([colidx(:), Values(:)],1) ./ size(Values,1);;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!