How to create a symmetric histogram

조회 수: 12 (최근 30일)
Lukos
Lukos 2019년 2월 26일
댓글: Lukos 2019년 2월 26일
Hello,
I have data vector A, which includes some angular values between 0 and 180 degrees.
I have the desire to get a symmetric histogram. Therefore I create an extra vector B, which is 180-vector A, so that the sum of vector of A and B is 180 degrees.
However, When I plot the Vector C which is C = [A;B]; it does not give me the desired resulted.
Any thoughts?

채택된 답변

Steven Lord
Steven Lord 2019년 2월 26일
Okay, now I see the problem you're experiencing, I think. Since this is a bit more technical I'm moving this from the comments of Sayyed Ahmad's Answer into its own Answer.
randn is most likely going to return numbers between say -3 and +3. Instead I'm going to generate some sample data that covers more of the region.
x = randi([0 180], 1000, 1);
data = [x; 180-x];
Now I'll choose some symmetric bins and make the histogram. You might expect this to make a symmetric histogram but it doesn't.
edges = 0:10:180;
h = histogram(data, edges);
So why isn't the histogram symmetric? Each bin except the last contains its left edge but not its right (the last bin contains both edges.) This means a value of 170 in data is part of the last bin in the histogram h (that's the left edge of that last bin.) However a value of 10 in data is not part of the first bin in h (it's the right edge of the first bin and the left edge of the second so it is in the second bin.) To handle this, you could either offset the bin edges so no data value falls exactly on an edge:
edges2 = [-0.5:10:89.5 90.5:10:180.5];
figure;
h2 = histogram(data, edges2);
Or, since my data contains only integer values, use the BinMethod that puts each integer in its own bin.
figure
h3 = histogram(data, 'BinMethod', 'integers');
  댓글 수: 1
Lukos
Lukos 2019년 2월 26일
Thanks! greatly appreciated.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sayyed Ahmad
Sayyed Ahmad 2019년 2월 26일
du you means this?
A=linspace(0,pi,181);
X=sin(A);
Y=-X
plot(A,X,'r-',A,Y,'b-')
  댓글 수: 5
Steven Lord
Steven Lord 2019년 2월 26일
x = randn(1, 100);
data = [x, -x];
h = histogram(data, -3:0.5:3);
That looks like a pretty symmetric histogram to me. Use this same technique for your data.
Lukos
Lukos 2019년 2월 26일
This is exactly what I do, However I introduce a shift, so I get:
x = randn(100,1);
x2 = 180 - x % PROVIDED THAT x(i)+x2(i)=180
data = [x;x2]
--> No symmetric histogram

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by