Count number of values of a Matrix inside a range and plot it

조회 수: 7 (최근 30일)
Andrea Mira
Andrea Mira 2021년 1월 14일
편집: Adam Danz 2021년 1월 15일
Hi!
I have a matrix A which dimension is 100 X 100.
I filtered A in order to obtain its values less than 15 and I obtain matrix B.
B=A;
indices = find(abs(B)>15);
B(indices) = NaN;
I would like to know how to obtain the number of values in B in the folowing ranges:
Number of values of B between 5 and 15
Number of values of B between 4 and 5
Number of values of B between 3 and 4
Number of values of B between 0 and 3
Finally I would like to plot these (numbers of values in these ranges) in a bar graph or hist graph.
Thank you!!

채택된 답변

Adam Danz
Adam Danz 2021년 1월 14일
편집: Adam Danz 2021년 1월 15일
bins = [0,3,4,5,15];
h = histogram(B(:),bins);
To get the counts within each bin,
h.Values
  댓글 수: 4
Andrea Mira
Andrea Mira 2021년 1월 15일
Thanks, that's what I want
and sorry for the inconvenience
Adam Danz
Adam Danz 2021년 1월 15일
편집: Adam Danz 2021년 1월 15일
No problem.
When using histogram (or histcounts) to count binned data, remember these points summarized from the documentation.
  1. For a bin [a,b], data are counted if a <= data < b so values that are equal to the upper bound of the bin are not counted in the bin.
  2. The last bin behaves differently. For bin [a,b], data are counted if a <= data <= b as is shown below.
bins = [1 2 3 4];
data = [1.0 1.5 2.0 2.5 3.0 3.5 4.0];
histogram(data, bins)
A solution to get around this problem is to add another bin:
bins = [1 2 3 4 5];
histogram(data, bins)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by