How to apply a diff to histogram object?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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.
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.
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
