confused about the output of hist

79 views Asked by At

I am confused by the

[m,n]=hist(y,x)

such as

M = [1, 2, 3;
     4, 5, 6;
     1, 2, 3];

[m,n] =  hist(M,1:3)

Which results in

 m = 2     0     0
     0     2     0
     1     1     3

Can someone please explain how m is calculated?

1

There are 1 answers

3
rst On BEST ANSWER

hist actually takes vectors as input arguments, you wrote a matrix, so it just handles your input as if it was several vector-inputs. The output are the number of elements for each container (in your case 1:3, the second argument).

[m,n] =  hist([1,2,3;4,5,6;1,2,3],1:3)

treats each column as one input. You put in 3 inputs (# of columns) and you get 3 outputs.

[2 0 1]' 

means, for the input [1;4;1] and the bin 1:3 two elements are in bin 1 and one element is in bin 3.

Look at the last column of m, here all three values are in the third bin, which makes sense, since the corresponding vector is [3;6;3], and out of those numbers all have to go into the bin/container 3.