How can I extract a distribution from a 2d histogram?

8 ビュー (過去 30 日間)
Jose Rueda Rueda
Jose Rueda Rueda 2019 年 5 月 6 日
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

採用された回答

darova
darova 2019 年 5 月 6 日
Look HERE and HERE
  1 件のコメント
Jose Rueda Rueda
Jose Rueda Rueda 2019 年 5 月 6 日
It could works, thank you

サインインしてコメントする。

その他の回答 (2 件)

Constantino Carlos Reyes-Aldasoro
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
  1 件のコメント
Jose Rueda Rueda
Jose Rueda Rueda 2019 年 5 月 6 日
Ok, maybe it is my fault because the thing what I want it is not exactly a marginal distribution.
The problem is that I have the XY plane with the counts in Z and for example I want to know how many counts are there along the line Y=X-2

サインインしてコメントする。


Constantino Carlos Reyes-Aldasoro
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)
untitled.jpg
Then you sum along one dimension and plot
>> plot(Y(:,3),sum(Z,2))
untitled2.jpg
the line gives you the counts and the value you want depends on the x-axis
  2 件のコメント
Jose Rueda Rueda
Jose Rueda Rueda 2019 年 5 月 6 日
Good night,
The idea that you propose it is ok, but it is not what I need, doing that I will sum (project) all the histogram over one of the axis, in your example X, But I do not want that, I want to count only the counts (Z) that I have for a given line in the XY plane not necesarely parallel to one of the axis. For example I want the counts on the line Y=2x+1 and nothing else
Constantino Carlos Reyes-Aldasoro
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.

サインインしてコメントする。

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by