How can I extract a distribution from a 2d histogram?
8 ビュー (過去 30 日間)
古いコメントを表示
Jose Rueda Rueda
2019 年 5 月 6 日
コメント済み: Constantino Carlos Reyes-Aldasoro
2019 年 5 月 7 日
How can I extract a distribution from a 2d histogram?
Good morning,
I have created a 2D histogram. That is straight forward, I have the 3 matrix, X Y the grid and Z, the number of counts in each cell of the grid.
Now it comes my question, the big histogram, (that you can see in the attached photo) gives me a lot of information but I need the marginal histogram, for example I want to know the number of counts (Z) but only along the line y=-2x+1.
Can you give me a bit of help?
Thank you very much in advance
0 件のコメント
採用された回答
その他の回答 (2 件)
Constantino Carlos Reyes-Aldasoro
2019 年 5 月 6 日
It is rather simple, X,Y have positions, so that does not count, it is Z the one that contains the information. To obtain a marginal distribution you need to add along one of the dimensions, so to obtain either distribution you just add
marginal_1 = sum (Z,1);
marginal_2 = sum (Z,2);
and that would give you the marginal distributions. One thing to take into account is that if you are considering probability distributions, the sum of all (in your case sum(sum(Z)) ) should add to one, thus the values in Z should always be lower than 1.
Hope this helps
Constantino Carlos Reyes-Aldasoro
2019 年 5 月 6 日
The concept is the same even if it is not a probability distribution, you only need to sum along one of the dimensions and the values will be the counts along one of the lines. For example:
[X,Y]=meshgrid(-5:0.5:5,-5:0.5:5);
Z=abs(randn(21)./(X.^2+Y.^2));
mesh(X,Y,Z)
Then you sum along one dimension and plot
>> plot(Y(:,3),sum(Z,2))
the line gives you the counts and the value you want depends on the x-axis
2 件のコメント
Constantino Carlos Reyes-Aldasoro
2019 年 5 月 7 日
OK, now I understand exactly what you mean. And, here is where it gets a bit more mathematically subtle: 1) you can do the interpolation of the points as suggested above, but that will give you more values than those that fullfil exactly the conditions that you want.
2) if you want just the values along a line in which X and Y met some condition, you need to address the matrix Z with the values of the combination between matrices X and Y, for example Y=2X+1, first combine X and Y like this
>> Y-2*X
ans =
Columns 1 through 7
5.0000 4.0000 3.0000 2.0000 1.0000 0 -1.0000
5.5000 4.5000 3.5000 2.5000 1.5000 0.5000 -0.5000
6.0000 5.0000 4.0000 3.0000 2.0000 1.0000 0
6.5000 5.5000 4.5000 3.5000 2.5000 1.5000 0.5000
7.0000 6.0000 5.0000 4.0000 3.0000 2.0000 1.0000
7.5000 6.5000 5.5000 4.5000 3.5000 2.5000 1.5000
8.0000 7.0000 6.0000 5.0000 4.0000 3.0000 2.0000
8.5000 7.5000 6.5000 5.5000 4.5000 3.5000 2.5000
9.0000 8.0000 7.0000 6.0000 5.0000 4.0000 3.0000
9.5000 8.5000 7.5000 6.5000 5.5000 4.5000 3.5000
10.0000 9.0000 8.0000 7.0000 6.0000 5.0000 4.0000
10.5000 9.5000 8.5000 7.5000 6.5000 5.5000 4.5000
11.0000 10.0000 9.0000 8.0000 7.0000 6.0000 5.0000
11.5000 10.5000 9.5000 8.5000 7.5000 6.5000 5.5000
12.0000 11.0000 10.0000 9.0000 8.0000 7.0000 6.0000
12.5000 11.5000 10.5000 9.5000 8.5000 7.5000 6.5000
13.0000 12.0000 11.0000 10.0000 9.0000 8.0000 7.0000
13.5000 12.5000 11.5000 10.5000 9.5000 8.5000 7.5000
14.0000 13.0000 12.0000 11.0000 10.0000 9.0000 8.0000
14.5000 13.5000 12.5000 11.5000 10.5000 9.5000 8.5000
15.0000 14.0000 13.0000 12.0000 11.0000 10.0000 9.0000
Columns 8 through 14
-2.0000 -3.0000 -4.0000 -5.0000 -6.0000 -7.0000 -8.0000
-1.5000 -2.5000 -3.5000 -4.5000 -5.5000 -6.5000 -7.5000
-1.0000 -2.0000 -3.0000 -4.0000 -5.0000 -6.0000 -7.0000
-0.5000 -1.5000 -2.5000 -3.5000 -4.5000 -5.5000 -6.5000
0 -1.0000 -2.0000 -3.0000 -4.0000 -5.0000 -6.0000
0.5000 -0.5000 -1.5000 -2.5000 -3.5000 -4.5000 -5.5000
1.0000 0 -1.0000 -2.0000 -3.0000 -4.0000 -5.0000
1.5000 0.5000 -0.5000 -1.5000 -2.5000 -3.5000 -4.5000
2.0000 1.0000 0 -1.0000 -2.0000 -3.0000 -4.0000
2.5000 1.5000 0.5000 -0.5000 -1.5000 -2.5000 -3.5000
3.0000 2.0000 1.0000 0 -1.0000 -2.0000 -3.0000
3.5000 2.5000 1.5000 0.5000 -0.5000 -1.5000 -2.5000
4.0000 3.0000 2.0000 1.0000 0 -1.0000 -2.0000
4.5000 3.5000 2.5000 1.5000 0.5000 -0.5000 -1.5000
5.0000 4.0000 3.0000 2.0000 1.0000 0 -1.0000
5.5000 4.5000 3.5000 2.5000 1.5000 0.5000 -0.5000
6.0000 5.0000 4.0000 3.0000 2.0000 1.0000 0
6.5000 5.5000 4.5000 3.5000 2.5000 1.5000 0.5000
7.0000 6.0000 5.0000 4.0000 3.0000 2.0000 1.0000
7.5000 6.5000 5.5000 4.5000 3.5000 2.5000 1.5000
8.0000 7.0000 6.0000 5.0000 4.0000 3.0000 2.0000
Columns 15 through 21
-9.0000 -10.0000 -11.0000 -12.0000 -13.0000 -14.0000 -15.0000
-8.5000 -9.5000 -10.5000 -11.5000 -12.5000 -13.5000 -14.5000
-8.0000 -9.0000 -10.0000 -11.0000 -12.0000 -13.0000 -14.0000
-7.5000 -8.5000 -9.5000 -10.5000 -11.5000 -12.5000 -13.5000
-7.0000 -8.0000 -9.0000 -10.0000 -11.0000 -12.0000 -13.0000
-6.5000 -7.5000 -8.5000 -9.5000 -10.5000 -11.5000 -12.5000
-6.0000 -7.0000 -8.0000 -9.0000 -10.0000 -11.0000 -12.0000
-5.5000 -6.5000 -7.5000 -8.5000 -9.5000 -10.5000 -11.5000
-5.0000 -6.0000 -7.0000 -8.0000 -9.0000 -10.0000 -11.0000
-4.5000 -5.5000 -6.5000 -7.5000 -8.5000 -9.5000 -10.5000
-4.0000 -5.0000 -6.0000 -7.0000 -8.0000 -9.0000 -10.0000
-3.5000 -4.5000 -5.5000 -6.5000 -7.5000 -8.5000 -9.5000
-3.0000 -4.0000 -5.0000 -6.0000 -7.0000 -8.0000 -9.0000
-2.5000 -3.5000 -4.5000 -5.5000 -6.5000 -7.5000 -8.5000
-2.0000 -3.0000 -4.0000 -5.0000 -6.0000 -7.0000 -8.0000
-1.5000 -2.5000 -3.5000 -4.5000 -5.5000 -6.5000 -7.5000
-1.0000 -2.0000 -3.0000 -4.0000 -5.0000 -6.0000 -7.0000
-0.5000 -1.5000 -2.5000 -3.5000 -4.5000 -5.5000 -6.5000
0 -1.0000 -2.0000 -3.0000 -4.0000 -5.0000 -6.0000
0.5000 -0.5000 -1.5000 -2.5000 -3.5000 -4.5000 -5.5000
1.0000 0 -1.0000 -2.0000 -3.0000 -4.0000 -5.0000
And say you want those values where the combination is 1, that would be:
Y-2*X==1
ans =
21×21 logical array
Columns 1 through 18
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
Columns 19 through 21
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
So, the last step is the address the matrix Z with the logical condition above:
>> Z(Y-2*X==1)
ans =
0.0010
0.0430
0.0972
0.0483
0.0533
0.4402
0.4405
0.0257
0.0915
0.0147
0.0191
>>
Thus you have only the values of the matrix Z where the values of (Y-2*X==1). The sum is then just :
>> sum(Z(Y-2*X==1))
ans =
1.2745
>>
Notice that if you interpolate, the result will be different as the interpolation will perform an operation, be it cubic, linear, etc. If you want exactly the values of the matrix Z, addressing it will give you the values. On the other hand, if you want values that do not exist in the matrices (say 1.15 in the example above) then there will be no valid address in the matrix and thus the answer will be an empty matrix:
>> Z(Y-2*X==1.15)
ans =
0×1 empty double column vector
>>
Anyway, with either method you will be able to solve your problem, but feel free to ask if you need more clarification.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!