How to apply a diff to histogram object?

Hi everybody,
I am trying to apply a difference to an histogram result, which is an object.
H=histogram(matrixvalues,bins);
dH=[0,diff(H)];
but the result is
Undefined function 'diff' for input arguments of type 'matlab.graphics.chart.primitive.Histogram'.
This is because, I think, the H output is an histogram object and not a double.
With the older function hist there are no problems because the output is a double, but with histogram I can not.
Any advices?
Thanks a lot

 Accepted Answer

I have no idea what you wwant.
Try this:
dH = diff(H.BinCounts);
To se all the available fields, do this:
histogramFields = get(H)

10 Comments

Yes, thank you, something like that.
I am trying with diff(H.Values), which the same of diff(H.BinCounts).
Another question, please:
I apply histogram on my data matrixvalues or X, making histogram in a vector with 0.1 step from 0 to 5
cl=[0:0.1:5];
h=histogram(X,cl)
The problem is that using histogram function I will get an h.Values or h.BinCounts (numb. of Bins) of 50 elements, while I want to have 51 elements discretized, as my vector cl length.
I think this is due because histogram function works for edges and not for center bins analysis, or somethinkg like that.
Does everybody know how to get an h.Values vector long as my bins vector, and not less of this?
Thank you!
As always, my pleasure.
I calculate the edges for the histogram functions using the linspace (link) function. For example, if you want 50 bins from 0 to 5, calculate the edges as:
edgs = linspace(0, 5, 51);
It creates a 51-element vector.
See if that does what you want.
Unfortunately, the problem is the same.
If I use:
edges=linspace(0, 5, 51);
h=histogram(X,edges)
the result is a h.Values vector with 50 NumBins and not 51, as the length of edges vector.
With the older hist function of matlab, it creates a vector with the same length of your bins vector.
H=hist(X,edges);
>> whos H
Name Size Bytes Class Attributes
H 1x51 408 double
I would like to use the more recent histogram function and to have an histogram vector with the same length of my bins vector.
I’m not certain I understand what you want.
This code:
X = rand(1, 100)*5;
edges=linspace(0, 5, 51);
figure
h1=histogram(X,edges)
nbins = 50;
figure
h2 = histogram(X,nbins)
produces these results for the histogram objects:
h1 =
Histogram with properties:
Data: [1×100 double]
Values: [1×50 double]
NumBins: 50
BinEdges: [1×51 double]
BinWidth: 0.1
BinLimits: [0 5]
. . .
and:
h2 =
Histogram with properties:
Data: [1×100 double]
Values: [1×50 double]
NumBins: 50
BinEdges: [1×51 double]
BinWidth: 0.1
BinLimits: [0 5]
. . .
so essentially the same result, and with identical histogram plots.
The problem is that I used the older hist function that works differently than the new histogram function. And I would like to replace the older one (hist) with the new histogram, encouraged by Matlab.
If I use hist
H=hist(X,edges);
>> whos H
Name Size Bytes Class Attributes
H 1x51 408 double
my H vectors is long 51.
If I use histogram
h=histogram(X,edges)
NumBins: 50
BinEdges: [1×51 double]
So the histogram operation made by the new function histogram work in a different manner. It works with a edges 51-elment vector but the result is a h.Values (or h.BinCounts) of 50-element.
The problem is already cited in other post, but I didn't find at the moment a feasible solution.
I hope I've been clear.
Thanks.
I’m still not certain that I understand.
What result do you want?
I want an histogram vector (h.Values or h.BinCounts) with the same length of my bins vector.
So, if I apply histogram for a edges vector of 51-element (BinEdges: [1×51 double]), I want as result an histogram with 51 NumBins and not 50 NumBins.
The older hist Matlab function produced an h vector of the same length of the bins.
Whilst, the newer histogram function produces an h.Values vector of num bins-1.
I want to replace the older hist with the newer histogram, maintaining the same result.
Thanks.
There is always one more edge than the number of bins. That is simply the way edges work. You cannot have the same number of bins as edges.
I believe you are mistaking the ‘xbins’ argument for the edges, as described in the hist documentation section Specify Histogram Bin Intervals (link). The histogram function does not have that capability.
You can calculate the bin midpoints easily enough with the movmean function:
midpts = movmean(edges,2)
midpts = midpts(2:end)
The problem arises in inverting that calculation, and creating edges from the midpoints. For regularly-spaced midpoints, this is straightforward:
mp = linspace(0, 1, 5); % Midpoint Vector
dmp = mean(diff(mp))/2;
edgs = [mp-dmp mp(end)+dmp]; % Edges Vector
figure
plot(mp, ones(size(mp)), '^r')
hold on
plot(edgs, ones(size(edgs)), '+b')
hold off
That is one way to construct your ‘edges’ vector from a regularly-spaced midpoints vector. (The solution for irregularly-spaced midpoints eludes me.)
It works! Great!
The only (little) problem is that in this way I have to add one edge more (for example the egds vector is a 52-element long), in order to obtain a h.Values of the same length of that produced by older hist function. So my egds vector will start from negative value, for exampe.
Anyway, thanks!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!