Switching from hist to histogram

84 vues (au cours des 30 derniers jours)
Louise Wilson
Louise Wilson le 10 Nov 2025 à 1:47
Modifié(e) : dpb le 12 Nov 2025 à 15:52
I am trying to make my code more robust by switching from use of histogram to hist.
Currently, I use hist as follows:
hist(psd,vec)
where psd is a matrix and vec is a vector.
The output of hist is a matrix where the height is the length of the vec data, and the width is the width of the psd data.
It gives me the values in psd, in bins with bin centres? listed by vec.
The output of histogram is instead an object, and I'm not sure how to derive the info I need from this object? Thanks

Réponse acceptée

dpb
dpb le 10 Nov 2025 à 14:33
Modifié(e) : dpb le 10 Nov 2025 à 18:36
The changes in behavior relative to venerable hist introduced by histogram are a pain, agreed. That there isn't an option to specify bin centers instead of only edges is an egregious omission. While one can convert the centers to edges, it's just another piece of minutiae to deal with instead of solving the underlying problem. At least they ought to have packaged a conversion function that implements the conversion outlined at <Convert Bin Centers to Edges>.
As for your usage, "if it ain't broke, don't fix it!" would be my recommendation. While deprecated for new code, there's no chance Mathworks can remove hist entirely and while it might be somewhat advantageous for new code to use the newer function, there really isn't any strong reason to work over current code unless there were some real advantage to the object that is presently difficult later on in your code besides just the shown code snippet usage.
$0.02, im(ns)ho, ymmv, etc., etc., ...
  7 commentaires
Image Analyst
Image Analyst le 12 Nov 2025 à 3:41
Modifié(e) : Image Analyst le 12 Nov 2025 à 3:41
If you have edges and want centers
centers = (edges(1:end-1) + edges(2:end)) / 2; % Centers are the average of the two edge locations.
If you have centers and bin widths and want edges:
numBins = numel(centers);
firstEdge = centers(1) - binWidth/2;
lastEdge = centers(end) + binWidth/2;
edges = linspace(firstEdge, lastEdge, numBins + 1);
dpb
dpb le 12 Nov 2025 à 13:04
Modifié(e) : dpb le 12 Nov 2025 à 15:52
Unfortunately, that doesn't quite reproduce what hist does in general...and it also fails for nonuniform binning.
>> x=randn(1000,1);
>> [min(x) max(x)]
ans =
-3.2320 3.5699
>> [n,c]=hist(x,-2:2)
n =
64 241 396 225 74
c =
-2 -1 0 1 2
>> sum(n)
ans =
1000
>>
The bin width is 1 so first edge would be -2.5 and last 2.5 and n=6...
>> e=linspace(-2.5,2.5,6)
e =
-2.5000 -1.5000 -0.5000 0.5000 1.5000 2.5000
>> h=histogram(x,e)
h =
Histogram with properties:
Data: [1000×1 double]
Values: [61 241 396 225 60]
NumBins: 5
BinEdges: [-2.5000 -1.5000 -0.5000 0.5000 1.5000 2.5000]
BinWidth: 1
BinLimits: [-2.5000 2.5000]
Normalization: 'count'
FaceColor: 'auto'
EdgeColor: [0 0 0]
Show all properties
>> h.BinCounts
ans =
61 241 396 225 60
>> sum(h.BinCounts)
ans =
983
>>
One has to mung on the end limits to capture the range.
>> e=e.*[inf ones(1,4) inf];
>> h=histogram(x,e)
h =
Histogram with properties:
Data: [1000×1 double]
Values: [64 241 396 225 74]
NumBins: 5
BinEdges: [-Inf -1.5000 -0.5000 0.5000 1.5000 Inf]
BinWidth: 'nonuniform'
BinLimits: [-Inf Inf]
Normalization: 'count'
FaceColor: 'auto'
EdgeColor: [0 0 0]
Show all properties
>> h.BinCounts
ans =
64 241 396 225 74
>> sum(h.BinCounts)
ans =
1000
>>
Even if the sample happened to not exceed +/-2.5, the count in each bin might be just a little bit different if an observation did happen to fall exactly on the bin edge as one sorts to the left, the other to the right. That's not a likely occurrence with randomly generated data as here, but could be if the data were sampled or rounded with a given precison, for example, and is a more subtle behavior difference. The boundaries are treated differently in substance, however.
One can live with either, but the conversion from one to the other isn't as trivial as Mathworks would have you believe.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by