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
採用された回答
Star Strider
2019 年 5 月 21 日
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 件のコメント
fransec
2019 年 5 月 22 日
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!
Star Strider
2019 年 5 月 22 日
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.
fransec
2019 年 5 月 23 日
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.
Star Strider
2019 年 5 月 23 日
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.
fransec
2019 年 5 月 23 日
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.
Star Strider
2019 年 5 月 23 日
I’m still not certain that I understand.
What result do you want?
fransec
2019 年 5 月 24 日
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.
Star Strider
2019 年 5 月 24 日
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.)
fransec
2019 年 5 月 27 日
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!
Star Strider
2019 年 5 月 27 日
As always, my pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Histograms についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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
- United Kingdom (English)
