Histogram bin counts - MATLAB histcounts - MathWorks France (2024)

Histogram bin counts

collapse all in page

Syntax

[N,edges]= histcounts(X)

[N,edges]= histcounts(X,nbins)

[N,edges]= histcounts(X,edges)

[N,edges,bin]= histcounts(___)

N = histcounts(C)

N = histcounts(C,Categories)

[N,Categories]= histcounts(___)

[___] = histcounts(___,Name,Value)

Description

example

[N,edges]= histcounts(X) partitions the X values into bins and returns the bin counts and the bin edges. The histcounts function uses an automatic binning algorithm that returns uniform bins chosen to cover the range of elements in X and reveal the underlying shape of the distribution.

example

[N,edges]= histcounts(X,nbins) usesa number of bins specified by the scalar, nbins.

example

[N,edges]= histcounts(X,edges) sorts X into bins with the bin edges specified by the vector, edges.

example

[N,edges,bin]= histcounts(___) also returns an index array, bin,using any of the previous syntaxes. bin is an arrayof the same size as X whose elements are the binindices for the corresponding elements in X. Thenumber of elements in the kth bin is nnz(bin==k),which is the same as N(k).

example

N = histcounts(C),where C is a categorical array, returns a vector, N,that indicates the number of elements in C whosevalue is equal to each of C’s categories. N hasone element for each category in C.

N = histcounts(C,Categories) countsonly the elements in C whose value is equal tothe subset of categories specified by Categories.

example

[N,Categories]= histcounts(___) also returns the categoriesthat correspond to each count in N using eitherof the previous syntaxes for categorical arrays.

example

[___] = histcounts(___,Name,Value) specifies additional parameters using one or more name-value arguments. For example, you can specify BinWidth as a scalar to adjust the width of the bins for numeric data.

Examples

collapse all

Bin Counts and Bin Edges

Open Live Script

Distribute 100 random values into bins. histcounts automatically chooses an appropriate bin width to reveal the underlying distribution of the data.

X = randn(100,1);[N,edges] = histcounts(X)
N = 1×7 2 17 28 32 16 3 2
edges = 1×8 -3 -2 -1 0 1 2 3 4

Specify Number of Bins

Open Live Script

Distribute 10 numbers into 6 equally spaced bins.

N = 1×6 2 2 2 2 1 1
edges = 1×7 0 4.9000 9.8000 14.7000 19.6000 24.5000 29.4000

Specify Bin Edges

Open Live Script

Distribute 1,000 random numbers into bins. Define the bin edges with a vector, where the first element is the left edge of the first bin, and the last element is the right edge of the last bin.

X = randn(1000,1);edges = [-5 -4 -2 -1 -0.5 0 0.5 1 2 4 5];N = histcounts(X,edges)
N = 1×10 0 24 149 142 195 200 154 111 25 0

Normalized Bin Counts

Open Live Script

Distribute all of the prime numbers less than 100 into bins. Specify 'Normalization' as 'probability' to normalize the bin counts so that sum(N) is 1. That is, each bin count represents the probability that an observation falls within that bin.

X = primes(100);[N,edges] = histcounts(X, 'Normalization', 'probability')
N = 1×4 0.4000 0.2800 0.2800 0.0400
edges = 1×5 0 30 60 90 120

Determine Bin Placement

Open Live Script

Distribute 100 random integers between -5 and 5 into bins, and specify 'BinMethod' as 'integers' to use unit-width bins centered on integers. Specify a third output for histcounts to return a vector representing the bin indices of the data.

X = randi([-5,5],100,1);[N,edges,bin] = histcounts(X,'BinMethod','integers');

Find the bin count for the third bin by counting the occurrences of the number 3 in the bin index vector, bin. The result is the same as N(3).

count = nnz(bin==3)
count = 8

Categorical Bin Counts

Open Live Script

Create a categorical vector that represents votes. The categories in the vector are 'yes', 'no', or 'undecided'.

A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];C = categorical(A,[1 0 NaN],{'yes','no','undecided'})
C = 1x27 categorical no no yes yes yes no no no no undecided undecided yes no no no yes no yes no yes no no no yes yes yes yes 

Determine the number of elements that fall into each category.

[N,Categories] = histcounts(C)
N = 1×3 11 14 2
Categories = 1x3 cell {'yes'} {'no'} {'undecided'}

Input Arguments

collapse all

XData to distribute among bins
vector | matrix | multidimensional array

Data to distribute among bins, specified as a vector, matrix,or multidimensional array. If X is not a vector,then histcounts treats it as a single column vector, X(:).

histcounts ignores all NaN values.Similarly, histcounts ignores Inf and -Inf valuesunless the bin edges explicitly specify Inf or -Inf asa bin edge.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | datetime | duration

CCategorical data
categorical array

Categorical data, specified as a categorical array. histcounts ignoresundefined categorical values.

Data Types: categorical

nbinsNumber of bins
positive integer

Number of bins, specified as a positive integer. If you do notspecify nbins, then histcounts automaticallycalculates how many bins to use based on the values in X.

Example: [N,edges] = histcounts(X,15) uses15 bins.

edgesBin edges
vector

Bin edges, specified as a vector. The first vector element specifies the leading edge of the first bin. The last element specifies the trailing edge of the last bin. The trailing edge is only included for the last bin.

For datetime and duration data, edges mustbe a datetime or duration vector in monotonically increasing order.

CategoriesCategories included in count
all categories (default) | string vector | cell vector of character vectors | pattern scalar | categorical vector

Categories included in count, specified as a string vector, cell vector of character vectors, pattern scalar, or categorical vector. By default, histcounts uses a bin for each category in categorical array C. Use Categories to specify a unique subset of the categories instead.

Example: h = histcounts(C,["Large","Small"]) counts only the categorical data in the categories Large and Small.

Example: h = histcounts(C,"Y" + wildcardPattern) counts categorical data in all the categories whose names begin with the letter Y.

Data Types: string | cell | pattern | categorical

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: [N,edges] = histcounts(X,'Normalization','probability') normalizesthe bin counts in N, such that sum(N) is1.

Output Arguments

collapse all

N — Bin counts
row vector

Bin counts, returned as a row vector.

edges — Bin edges
vector

Bin edges, returned as a vector. The first element is the leading edge of the first bin. The last element is the trailing edge of the last bin.

bin — Bin indices
array

Bin indices, returned as an array of the same size as X.Each element in bin describes which numbered bincontains the corresponding element in X.

A value of 0 in bin indicatesan element which does not belong to any of the bins (for example,a NaN value).

Categories — Categories included in count
cell vector of character vectors

Categories included in count, returned as a cell vector of charactervectors. Categories contains the categories in C thatcorrespond to each count in N.

Tips

  • The behavior of histcounts issimilar to that of the discretize function. Use histcounts tofind the number of elements in each bin. On the other hand, use discretize tofind which bin each element belongs to (without counting).

Extended Capabilities

Version History

Introduced in R2014b

expand all

You can normalize histogram values as percentages by specifying the Normalization name-value argument as 'percentage'.

See Also

histogram | histogram2 | discretize | histcounts2 | kde

Topics

  • Replace Discouraged Instances of hist and histc

Commande MATLAB

Vous avez cliqué sur un lien qui correspond à cette commande MATLAB:

 

Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.

Histogram bin counts - MATLAB histcounts- MathWorks France (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Histogram bin counts - MATLAB histcounts
- MathWorks France (2024)

FAQs

How to get the count from histogram in Matlab? ›

[ N , edges ] = histcounts( X ) partitions the X values into bins and returns the bin counts and the bin edges. The histcounts function uses an automatic binning algorithm that returns uniform bins chosen to cover the range of elements in X and reveal the underlying shape of the distribution.

What is the difference between histogram and Histcounts in Matlab? ›

Both histogram and histcounts have automatic binning and normalization capabilities, with several common options built-in. histcounts is the primary calculation function for histogram . The result is that the functions have consistent behavior.

How many bins can a histogram have Matlab? ›

If you specify BinWidth , then Histogram can use a maximum of 65,536 bins (or 216). If the specified bin width requires more bins, then histogram uses a larger bin width corresponding to the maximum number of bins.

How do you know how many values are in a histogram? ›

Figure 1: The anatomy of a histogram.

Histograms are visual representations of a data set which show how often each value in the data set occurs. The values are grouped into bins along the x-axis. The height of the bar indicates how many values of the data set fall into that bin.

What is histogram counts? ›

Definition. The most common form of the histogram is obtained by splitting the range of the data into equal-sized bins (called classes). Then for each bin, the number of points from the data set that fall into each bin are counted. That is. Vertical axis: Frequency (i.e., counts for each bin)

How do I determine the number of bins for binning? ›

Bins are the number of intervals you want to divide all of your data into, such that it can be displayed as bars on a histogram. A simple method to work our how many bins are suitable is to take the square root of the total number of values in your distribution.

What is the Bincount in Matlab? ›

bincounts = histc( x , binranges ) counts the number of values in x that are within each specified bin range. The input, binranges , determines the endpoints for each bin. The output, bincounts , contains the number of elements from x in each bin.

How do you read a histogram with bins? ›

Separate the data into the bins.

Then draw a line at the division of the bins. Count the number of values that fall into each bin. This number is the frequency of each range. Remember, if the value is equal to the boundary of a bin, it falls in the bin to the right.

How to use hist in MATLAB? ›

hist( x , xbins ) sorts x into bins with intervals or categories determined by the vector xbins .
  1. If xbins is a vector of evenly spaced values, then hist uses the values as the bin centers.
  2. If xbins is a vector of unevenly spaced values, then hist uses the midpoints between consecutive values as the bin edges.

How to plot histogram in MATLAB without using hist function? ›

Direct link to this question
  1. Pie=sin(2*pi*tt); %array of data p=sin(2*pi*t/T)
  2. L=10; %number of bins.
  3. minPie = min(Pie); %min value of array.
  4. maxPie = max(Pie); %max value of array.
  5. binwidth = (maxPie - minPie)/L; %width of bin.
  6. binnum = 1+floor((Pie - minPie) / binwidth); %value of array to a bin.
Jan 28, 2022

What are bin edges in a histogram? ›

A histogram plots the number of observations for each range of the values of the numeric feature. These ranges are called as Bins. The shape of the distribution depends on the size (width or edges) of the bins.

What is the formula for the number of bins in a histogram? ›

So the number of bins is (max−min)/h, where n is the number of observations, max is the maximum value and min is the minimum value. If you use too few bins, the histogram doesn't really portray the data very well.

What happens if your histogram has too few bins? ›

On the other hand, with too few bins, the histogram will lack the details needed to discern any useful pattern from the data. The left panel's bins are too small, implying a lot of spurious peaks and troughs.

How many bins is good for histogram? ›

Choose between 5 and 20 bins. The larger the data set, the more likely you'll want a large number of bins. For example, a set of 12 data pieces might warrant 5 bins but a set of 1000 numbers will probably be more useful with 20 bins. The exact number of bins is usually a judgment call.

How do you work out a number from a histogram? ›

Step 1: For each bar on our histogram, multiply the category (number) by the height of our bar (how many of each number we have). Step 2: Add each of the products determined in Step 1 together to get the total sum of our data. Step 3: Divide this sum by the sum of the heights of the bars to get our mean.

What can you calculate from a histogram? ›

The class width can be calculated by subtracting the lower boundary from the upper boundary, for example, 40 - 30 = 10 . The frequency density is calculated by dividing the frequency by the class width, for example, 6 .

Is there a count function in MATLAB? ›

Description. A = count( str , pat ) returns the number of occurrences of pat in str . If pat is an array containing multiple patterns, then count returns the sum of the occurrences of all elements of pat in str .

Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 5759

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.