how to create histogram for each value of a matrix

16 ビュー (過去 30 日間)
adi
adi 2020 年 5 月 13 日
コメント済み: adi 2020 年 5 月 14 日
Hi all,
I have a 300X332 double matrix and i need to have an histogram of each value from the matrix in precentage. i'll explain by a simple example:
assume i have M=[1,1,2,3; 0,4,2,2; 1,1,1,1] iwant to have an histogram that will tell me how many times i have each value of the matrix out of all matrix veriables in precentage. for example the value of 1 will have a bin with height of 50%.
i used histogram func and can get only bins that are wider than only one value - bins of value 1 between 2 then 3 between 4 and so on..
later i need to extract the matrix values that correspond to 80%-90% of the whole matrix
how can i do it?
thanks

回答 (1 件)

Ruger28
Ruger28 2020 年 5 月 13 日
編集済み: Ruger28 2020 年 5 月 13 日
M=[1,1,2,3; 0,4,2,2; 1,1,1,1];
N = M(:); % make into vector
[SortedMat,I] = sort(N); % sort data
[UnVals,uIDX] = unique(SortedMat); % get unique values
P = histcounts(SortedMat); % get number of times the number appears
num_of_elems = numel(M); % get total number of values in matrix
PercentVals = P / num_of_elems; % divide to get percent of matrix
bar(UnVals,PercentVals); % plot
xlabel('Values');
ylabel('Percentage [%]');
  1 件のコメント
adi
adi 2020 年 5 月 14 日
hi, i tried your code, but when i imply my code it doesnt work, p after the histcounts isnt the same length as UnVals. I did exactly the same as you did,just replaced M by Gmag of size 137X367.
i attached an image of my workspace.
what is wrong?
thank you!!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by