hist3 not returning expected output (weird range for indices etc)
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I was trying to use hist3 to visually display a two-dimensional distribution, however the function behaves unexpectedly. I expected it to return a 2D histogram (a count) of how many times each pair of numbers in the input matrix occurs, and display that count at a position in the output matrix whose indices correspond to that number pair, scaled to a factor of 10 (by default) or some other square-matrix size. But this is not what my test examples showed:
For instance, if I define the following input matrix m:
1 1
2 2
3 3
4 4
5 5
then the command v=hist3(m, [5 5]) has the following expected output:
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
However for this input matrix:
4 1
4 2
4 3
4 4
4 5
the output is:
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
0 0 0 0 0
..rather than (as I would have expected):
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
Also, hist3(m,[5 5]) returns a plot where one of the 2 plane dimensions ranges from 2-6 rather than 1-5:
..while hist3(m) returns the even more unexpected plot:
Can anyone help clear the confusion? THanks!
0 commentaires
Réponse acceptée
Massimo Zanetti
le 29 Sep 2016
Modifié(e) : Massimo Zanetti
le 29 Sep 2016
The behaviour of hist3 is correct. Think about this: in the first dimension (say "x") you only have one coordinate number that repeats: 4. In the other dimension "y" you have a range of values from 1 to 5.
So, how Matlab decides to bin in the "x" dimension? It has to divide in 5 bins a range of values which only consists of one number :) Thus, it "earns" the scale of values from "y" dimension, and use that scale also in the "x" dimension, and it put the number 4 in the middle (this is the most reasonable choice). So, the final range of values in the "x" dimension is 2-6, because 4 is in the middle. Is that clear now?
To get it more clear, look the bin centers Matlab has computed in the first dimension:
X=[4,4,4,4,4;1,2,3,4,5]';
[N,C]=hist3(X,[5,5]);
%bin centers in the first dimension "x":
%C{1} = [2,3,4,5,6]
%bin centers in the second dimension "y":
%C{2} = [1.4,2.2,3.0 3.8 4.6]
You can see, the 4 number, falls in the middle bin :)
3 commentaires
Massimo Zanetti
le 29 Sep 2016
Modifié(e) : Massimo Zanetti
le 29 Sep 2016
I think the best for you is to provide specifically the edges of the bins as argument of the hist3. This way you know the output, without "hacking" :)
Try this:
X=[4,4,4,4,4;1,2,3,4,5]';
edges = {.5:1:5.5,.5:1:5.5};
hist3(X,'Edges',edges);
If this answer helped you, please accept it.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Distribution Plots dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!