count matrix elements in defined intervals by histogram

조회 수: 2 (최근 30일)
ali eskandari
ali eskandari 2021년 7월 26일
편집: ali eskandari 2021년 7월 26일
In the attached matrix, I want to extract the ratios of matrix elements at defined intervals. In order to do that, I have written the code below, but I want to know is it possible to do that with a histogram function or any other built-in Matlab function?
cnt0 = 0;
cnt1 = 0;
cnt2 = 0;
cnt3 = 0;
cnt4 = 0;
cnt5 = 0;
for i = 1:numel(A)
if A(i) == 0
cnt0 = cnt0 + 1;
elseif A(i) > 0 && A(i) < 0.25
cnt1 = cnt1 + 1;
elseif A(i) >= 0.25 && A(i) < 0.5
cnt2 = cnt2 + 1;
elseif A(i) >= 0.5 && A(i) < 0.75
cnt3 = cnt3 + 1;
elseif A(i) >= 0.75 && A(i) < 1
cnt4 = cnt4 + 1;
elseif A(i) == 1
cnt5 = cnt5 + 1;
end
end
ratio = [cnt0/numel(A), cnt1/numel(A), cnt2/numel(A), cnt3/numel(A), cnt4/numel(A), cnt5/numel(A)]';

답변 (1개)

Steven Lord
Steven Lord 2021년 7월 26일
Use the histogram function (if you want to see the plot) or the histcounts function (if you just want the bin counts.) The 'Normalization' option for either function will also be of interest to you.
  댓글 수: 1
ali eskandari
ali eskandari 2021년 7월 26일
편집: ali eskandari 2021년 7월 26일
Thanks, but I don't know how can I define these intervals on histogram or histcounts function.
A = [0 0 0.1 0.25 0.4 0.5 0.6 0.75 0.9 1 1];
edges = [0 0.25 0.5 0.75 1];
N = histcounts(A,edges)
N =
3 2 2 4
it will count any value greater than or equal to 0 and less than 0.25, but I want to separate zeros and greater than 0.

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

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by