Replace Discouraged Instances of hist and histc - MATLAB & Simulink (2024)

Replace Discouraged Instances of hist and histc

Old Histogram Functions (hist, histc)

Earlier versions of MATLAB® use the hist and histc functions as the primary way to create histograms and calculate histogram bin counts. These functions, while good for some general purposes, have limited overall capabilities. The use of hist and histc in new code is discouraged for these reasons (among others):

  • After using hist to create a histogram, modifying properties of the histogram is difficult and requires recomputing the entire histogram.

  • The default behavior of hist is to use 10 bins, which is not suitable for many data sets.

  • Plotting a normalized histogram requires manual computations.

  • hist and histc do not have consistent behavior.

Recommended Histogram Functions

The histogram, histcounts, and discretize functions dramatically advance the capabilities of histogram creation and calculation in MATLAB, while still promoting consistency and ease of use. histogram, histcounts, and discretize are the recommended histogram creation and computation functions for new code.

Of particular note are the following changes, which stand as improvements over hist and histc:

  • histogram can return a histogram object. You can use the object to modify properties of the histogram.

  • 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.

  • discretize provides additional options and flexibility for determining the bin placement of each element.

Differences Requiring Code Updates

Despite the aforementioned improvements, there are several important differences between the old and now recommended functions, which might require updating your code. The tables summarize the differences between the functions and provide suggestions for updating code.

Code Updates for hist

DifferenceOld behavior with histNew behavior with histogram

Input matrices

hist creates a histogram for each column of an input matrix and plots the histograms side-by-side in the same figure.

A = randn(100,2);hist(A)

histogram treats the input matrix as a single tall vector and creates a single histogram. To plot multiple histograms, create a different histogram object for each column of data. Use the hold on command to plot the histograms in the same figure.

A = randn(100,2);h1 = histogram(A(:,1),10)edges = h1.BinEdges;hold onh2 = histogram(A(:,2),edges)

The above code example uses the same bin edges for each histogram, but in some cases it is better to set the BinWidth of each histogram to be the same instead. Also, for display purposes, it might be helpful to set the FaceAlpha property of each histogram, as this affects the transparency of overlapping bars.

Bin specification

hist accepts the bin centers as a second input.

histogram accepts the bin edges as a second input.

To convert bin centers into bin edges for use with histogram, see Convert Bin Centers to Bin Edges.

Note

In cases where the bin centers used with hist are integers, such as hist(A,-3:3), use the new built-in binning method of histogram for integers.

histogram(A,'BinLimits',[-3,3],'BinMethod','integers')

Output arguments

hist returns the bin counts as an output argument, and optionally can return the bin centers as a second output argument.

A = randn(100,1);[N, Centers] = hist(A)

histogram returns a histogram object as an output argument. The object contains many properties of interest (bin counts, bin edges, and so on). You can modify aspects of the histogram by changing its property values. For more information, see histogram.

A = randn(100,1);h = histogram(A);N = h.ValuesEdges = h.BinEdges

Note

To calculate bin counts (without plotting a histogram), replace [N, Centers] = hist(A) with [N,edges] = histcounts(A,nbins).

Default number of bins

hist uses 10 bins by default.

Both histogram and histcounts use an automatic binning algorithm by default. The number of bins is determined by the size and spread of the input data.

A = randn(100,1);histogram(A)histcounts(A)

Bin limits

hist uses the minimum and maximum finite data values to determine the left and right edges of the first and last bar in the plot. -Inf and Inf are included in the first and last bin, respectively.

If BinLimits is not set, then histogram uses rational bin limits based on, but not exactly equal to, the minimum and maximum finite data values. histogram ignores Inf values unless one of the bin edges explicitly specifies Inf or -Inf as a bin edge.

To reproduce the results of hist(A) for finite data (no Inf values), use 10 bins and explicitly set BinLimits to the minimum and maximum data values.

A = randi(5,100,1);histogram(A,10,'BinLimits',[min(A) max(A)])

Code Updates for histc

DifferenceOld behavior with histcNew behavior with histcounts
Input matrices

histc calculates the bin counts for each column of input data. For an input matrix of size m-by-n, histc returns a matrix of bin counts of size length(edges)-by-n.

A = randn(100,10);edges = -4:4;N = histc(A,edges)

histcounts treats the input matrix as a single tall vector and calculates the bin counts for the entire matrix.

A = randn(100,10);edges = -4:4;N = histcounts(A,edges)

Use a for-loop to calculate bin counts over each column.

A = randn(100,10);nbins = 10;N = zeros(nbins, size(A,2));for k = 1:size(A,2) N(:,k) = histcounts(A(:,k),nbins);end

If performance is a problem due to a large number of columns in the matrix, then consider continuing to use histc for the column-wise bin counts.

Values included in last bin

histc includes an element A(i) in the last bin if A(i) == edges(end). The output, N, is a vector with length(edges) elements containing the bin counts. Values falling outside the bins are not counted.

histcounts includes an element A(i) in the last bin if edges(end-1) <= A(i) <= edges(end). In other words, histcounts combines the last two bins from histc into a single final bin. The output, N, is a vector with length(edges)-1 elements containing the bin counts. If you specify the bin edges, then values falling outside the bins are not counted. Otherwise, histcounts automatically determines the proper bin edges to use to include all of the data.

A = 1:4;edges = [1 2 2.5 3]N = histcounts(A)N = histcounts(A,edges)

The last bin from histc is primarily useful to count integers. To do this integer counting with histcounts, use the 'integers' bin method:

N = histcounts(A,'BinMethod','integers'); 
Output arguments

histc returns the bin counts as an output argument, and optionally can return the bin indices as a second output argument.

A = randn(15,1);edges = -4:4;[N,Bin] = histc(A,edges)
  • For bin count calculations like N = histc(A,edges) or [N,bin] = histc(A,edges), use histcounts. The histcounts function returns the bin counts as an output argument, and optionally can return the bin edges as a second output, or the bin indices as a third output.

    A = randn(15,1);[N,Edges,Bin] = histcounts(A)
  • For bin placement calculations like [~,Bin] = histc(A,edges), use discretize. The discretize function offers additional options for determining the bin placement of each element.

    A = randn(15,1);edges = -4:4;Bin = discretize(A,edges)

Convert Bin Centers to Bin Edges

Open Live Script

The hist function accepts bin centers, whereas the histogram function accepts bin edges. To update code to use histogram, you might need to convert bin centers to bin edges to reproduce results achieved with hist.

For example, specify bin centers for use with hist. These bins have a uniform width.

A = [-9 -6 -5 -2 0 1 3 3 4 7];centers = [-7.5 -2.5 2.5 7.5];hist(A,centers)

Replace Discouraged Instances of hist and histc- MATLAB & Simulink (1)

To convert the bin centers into bin edges, calculate the midpoint between consecutive values in centers. This method reproduces the results of hist for both uniform and nonuniform bin widths.

d = diff(centers)/2;edges = [centers(1)-d(1), centers(1:end-1)+d, centers(end)+d(end)];

The hist function includes values falling on the right edge of each bin (the first bin includes both edges), whereas histogram includes values that fall on the left edge of each bin (and the last bin includes both edges). Shift the bin edges slightly to obtain the same bin counts as hist.

edges(2:end) = edges(2:end)+eps(edges(2:end))
edges = 1×5 -10.0000 -5.0000 0.0000 5.0000 10.0000

Now, use histogram with the bin edges.

histogram(A,edges)

Replace Discouraged Instances of hist and histc- MATLAB & Simulink (2)

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Replace Discouraged Instances of hist and histc- MATLAB & Simulink (3)

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

Replace Discouraged Instances of hist and histc
- MATLAB & Simulink (2024)

FAQs

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

The hist function includes values falling on the right edge of each bin (the first bin includes both edges), whereas histogram includes values that fall on the left edge of each bin (and the last bin includes both edges). Shift the bin edges slightly to obtain the same bin counts as hist .

What is the function of Histc? ›

Computes the histogram of a tensor. The elements are sorted into equal width bins between min and max . If min and max are both zero, the minimum and maximum values of the data are used.

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.

What is the function to achieve histogram matching in Matlab? ›

J = imhistmatch( I , ref ) adjusts the histogram of the 2-D grayscale or truecolor image I such that the histogram approximately matches the histogram of the reference image ref .

Why is histogram better? ›

Histograms are good for showing general distributional features of dataset variables. You can see roughly where the peaks of the distribution are, whether the distribution is skewed or symmetric, and if there are any outliers.

What is the advantage of a histogram compared to a dot plot? ›

Data can be represented in various ways such as dot plots, histograms, and box plots. Dot plots and box plots are useful for finding the median, while histograms are great for showing the number of values within a specific range.

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.

Why Repmat is used in Matlab? ›

The MATLAB® command repmat is used to replicate and tile arrays. It works on the built-in objects of MATLAB, namely double , char , as well as the generalized container objects cell and struct . The identical functionality is provided for replicating and tiling uncertain elements ( ureal , ultidyn , etc.)

How to check for missing data in Matlab? ›

To find missing values in a structure array, apply ismissing to each field in the structure by using the structfun function. To find missing values in a cell array of non-character vectors, apply ismissing to each cell in the cell array by using the cellfun function.

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.

What is the bin of a histogram? ›

A histogram displays numerical data by grouping data into "bins" of equal width. Each bin is plotted as a bar whose height corresponds to how many data points are in that bin. Bins are also sometimes called "intervals", "classes", or "buckets".

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 does the hist function do in Matlab? ›

Description. hist( x ) creates a histogram bar chart of the elements in vector x . The elements in x are sorted into 10 equally spaced bins along the x-axis between the minimum and maximum values of x . hist displays bins as rectangles, such that the height of each rectangle indicates the number of elements in the bin.

What is the difference between histogram and histogram? ›

A histogram shows us the frequency distribution of continuous variables. In contrast, a bar graph refers to a diagrammatic comparison of discrete variables. The histogram represents numerical data whereas the bar graph represents categorical data.

What is the difference between a histogram and a quantile plot? ›

Histograms show the distribution of data, while Q-Q plots compare observed data points to what would be expected in a normal distribution. Statistical tests, such as those mentioned (skewness, kurtosis), can quantitatively assess whether the data deviates significantly from a normal distribution.

Top Articles
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 5749

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.