Creating 1D array from frequency data/histogram?
6 ビュー (過去 30 日間)
古いコメントを表示
I’m trying to run an algorithm that requires a 1 dimensional data to work. My data is grey values from ct images describing the frequency of 8bit pixel values from 0-255. Is it possible to create an 1D array [0,1,2,3,4,5...255] with corresponding frequency of that pixel value?
0 件のコメント
回答 (2 件)
Steven Lord
2018 年 6 月 27 日
If you want the resulting array to contain exactly n(1) copies of x(1), n(2) copies of x(2), etc. use the repelem function. [Depending on how you want to use the result, you may also want to "shuffle" it using randperm.]
x = 0:5;
freq = [2 3 4 5 6 7];
A = repelem(x, freq);
If instead of frequencies you have probabilities for what each element of the resulting array should be, use discretize.
p = freq./sum(freq);
cumulativeprob = [0 cumsum(p)];
B = discretize(rand(1, 1000), cumulativeprob, x);
Let's check that the proportions of elements in B come close to the probabilities in p.
c = histcounts(B, 'BinEdges', [x x(end)], 'Normalization', 'probability');
[c; p]
The values in c look reasonably close to the probabilities in p for just 1000 samples.
0 件のコメント
Gayatri Menon
2018 年 6 月 27 日
Hi,
Could you check out "histogram" command. Please refer to the below documentation to know more on this comment:
Hope this helps.
Gayatri
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Histograms についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!