Finding where two histograms cross paths - MATLAB












1















I am generating two histograms using the histogram function from Matlab that are both normalized using the probability argument.



However, once I generate two histograms as shown below, I'd like to be able to find the exact point at which the histograms would cross paths, assuming the histograms were drawn using lines instead of bars. Unfortunately, this form of histogram doesn't allow for lines, it just has bars. There is a hist function which can be manipulated in Matlab to draw a histogram as lines instead of bars, however, it doesn't easily normalize.



Hence, ideally, I'd like to use histogram() to plot the 2 histograms and find where they cross. See image below:



enter image description here



Here's an example of how the graphs can be created:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;


Now from here, I want to find the point where the two histograms cross paths. Note, the intersection value is the intersection (in the mathematical sense). This is not what I'm looking for. I'm looking for the x coordinate of where the two histograms cross at their outer boundaries. For example, in the attached image, the answer would be ~2.5.










share|improve this question




















  • 1





    If the bars are the same width and centered in the same values then its relatively eas, just iterate over all bars and compare them to their matching. Otherwise, you are in "very long code, you need to try something yourself before asking" waters. Can you show a Minimal, Complete, and Verifiable example of how you generate these histograms?

    – Ander Biguri
    Nov 21 '18 at 9:50








  • 1





    And how do you get intersection at 0.66? x-axis is 2.something and y-axis 0.27ish.

    – kkuilla
    Nov 21 '18 at 9:57











  • @kkuilla ignore the intersection value. Thats the intersection of the two histograms, not the point at which the lines cross, if that makes sense.

    – Jonathan
    Nov 21 '18 at 17:42






  • 1





    @AnderBiguri I've added some code, does this help?

    – Jonathan
    Nov 21 '18 at 17:46
















1















I am generating two histograms using the histogram function from Matlab that are both normalized using the probability argument.



However, once I generate two histograms as shown below, I'd like to be able to find the exact point at which the histograms would cross paths, assuming the histograms were drawn using lines instead of bars. Unfortunately, this form of histogram doesn't allow for lines, it just has bars. There is a hist function which can be manipulated in Matlab to draw a histogram as lines instead of bars, however, it doesn't easily normalize.



Hence, ideally, I'd like to use histogram() to plot the 2 histograms and find where they cross. See image below:



enter image description here



Here's an example of how the graphs can be created:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;


Now from here, I want to find the point where the two histograms cross paths. Note, the intersection value is the intersection (in the mathematical sense). This is not what I'm looking for. I'm looking for the x coordinate of where the two histograms cross at their outer boundaries. For example, in the attached image, the answer would be ~2.5.










share|improve this question




















  • 1





    If the bars are the same width and centered in the same values then its relatively eas, just iterate over all bars and compare them to their matching. Otherwise, you are in "very long code, you need to try something yourself before asking" waters. Can you show a Minimal, Complete, and Verifiable example of how you generate these histograms?

    – Ander Biguri
    Nov 21 '18 at 9:50








  • 1





    And how do you get intersection at 0.66? x-axis is 2.something and y-axis 0.27ish.

    – kkuilla
    Nov 21 '18 at 9:57











  • @kkuilla ignore the intersection value. Thats the intersection of the two histograms, not the point at which the lines cross, if that makes sense.

    – Jonathan
    Nov 21 '18 at 17:42






  • 1





    @AnderBiguri I've added some code, does this help?

    – Jonathan
    Nov 21 '18 at 17:46














1












1








1








I am generating two histograms using the histogram function from Matlab that are both normalized using the probability argument.



However, once I generate two histograms as shown below, I'd like to be able to find the exact point at which the histograms would cross paths, assuming the histograms were drawn using lines instead of bars. Unfortunately, this form of histogram doesn't allow for lines, it just has bars. There is a hist function which can be manipulated in Matlab to draw a histogram as lines instead of bars, however, it doesn't easily normalize.



Hence, ideally, I'd like to use histogram() to plot the 2 histograms and find where they cross. See image below:



enter image description here



Here's an example of how the graphs can be created:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;


Now from here, I want to find the point where the two histograms cross paths. Note, the intersection value is the intersection (in the mathematical sense). This is not what I'm looking for. I'm looking for the x coordinate of where the two histograms cross at their outer boundaries. For example, in the attached image, the answer would be ~2.5.










share|improve this question
















I am generating two histograms using the histogram function from Matlab that are both normalized using the probability argument.



However, once I generate two histograms as shown below, I'd like to be able to find the exact point at which the histograms would cross paths, assuming the histograms were drawn using lines instead of bars. Unfortunately, this form of histogram doesn't allow for lines, it just has bars. There is a hist function which can be manipulated in Matlab to draw a histogram as lines instead of bars, however, it doesn't easily normalize.



Hence, ideally, I'd like to use histogram() to plot the 2 histograms and find where they cross. See image below:



enter image description here



Here's an example of how the graphs can be created:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;


Now from here, I want to find the point where the two histograms cross paths. Note, the intersection value is the intersection (in the mathematical sense). This is not what I'm looking for. I'm looking for the x coordinate of where the two histograms cross at their outer boundaries. For example, in the attached image, the answer would be ~2.5.







matlab histogram






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:45







Jonathan

















asked Nov 21 '18 at 8:41









JonathanJonathan

422122




422122








  • 1





    If the bars are the same width and centered in the same values then its relatively eas, just iterate over all bars and compare them to their matching. Otherwise, you are in "very long code, you need to try something yourself before asking" waters. Can you show a Minimal, Complete, and Verifiable example of how you generate these histograms?

    – Ander Biguri
    Nov 21 '18 at 9:50








  • 1





    And how do you get intersection at 0.66? x-axis is 2.something and y-axis 0.27ish.

    – kkuilla
    Nov 21 '18 at 9:57











  • @kkuilla ignore the intersection value. Thats the intersection of the two histograms, not the point at which the lines cross, if that makes sense.

    – Jonathan
    Nov 21 '18 at 17:42






  • 1





    @AnderBiguri I've added some code, does this help?

    – Jonathan
    Nov 21 '18 at 17:46














  • 1





    If the bars are the same width and centered in the same values then its relatively eas, just iterate over all bars and compare them to their matching. Otherwise, you are in "very long code, you need to try something yourself before asking" waters. Can you show a Minimal, Complete, and Verifiable example of how you generate these histograms?

    – Ander Biguri
    Nov 21 '18 at 9:50








  • 1





    And how do you get intersection at 0.66? x-axis is 2.something and y-axis 0.27ish.

    – kkuilla
    Nov 21 '18 at 9:57











  • @kkuilla ignore the intersection value. Thats the intersection of the two histograms, not the point at which the lines cross, if that makes sense.

    – Jonathan
    Nov 21 '18 at 17:42






  • 1





    @AnderBiguri I've added some code, does this help?

    – Jonathan
    Nov 21 '18 at 17:46








1




1





If the bars are the same width and centered in the same values then its relatively eas, just iterate over all bars and compare them to their matching. Otherwise, you are in "very long code, you need to try something yourself before asking" waters. Can you show a Minimal, Complete, and Verifiable example of how you generate these histograms?

– Ander Biguri
Nov 21 '18 at 9:50







If the bars are the same width and centered in the same values then its relatively eas, just iterate over all bars and compare them to their matching. Otherwise, you are in "very long code, you need to try something yourself before asking" waters. Can you show a Minimal, Complete, and Verifiable example of how you generate these histograms?

– Ander Biguri
Nov 21 '18 at 9:50






1




1





And how do you get intersection at 0.66? x-axis is 2.something and y-axis 0.27ish.

– kkuilla
Nov 21 '18 at 9:57





And how do you get intersection at 0.66? x-axis is 2.something and y-axis 0.27ish.

– kkuilla
Nov 21 '18 at 9:57













@kkuilla ignore the intersection value. Thats the intersection of the two histograms, not the point at which the lines cross, if that makes sense.

– Jonathan
Nov 21 '18 at 17:42





@kkuilla ignore the intersection value. Thats the intersection of the two histograms, not the point at which the lines cross, if that makes sense.

– Jonathan
Nov 21 '18 at 17:42




1




1





@AnderBiguri I've added some code, does this help?

– Jonathan
Nov 21 '18 at 17:46





@AnderBiguri I've added some code, does this help?

– Jonathan
Nov 21 '18 at 17:46












1 Answer
1






active

oldest

votes


















1














From your example data, with a simple modification:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h1.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];

h2.Normalization = 'probability';
h2.BinWidth = 0.25;
h2.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];


data1=h1.Values;
data2=h2.Values;
intersection_value=find(data2>data1,1); % this is the index, bad variable name





share|improve this answer


























  • Hmm, this gives me a different answer than expected. When I ran this, I get an intersection value of ~15 everytime. However, the paths clearly cross [0,1]. I've updated my answer to make this more clear.

    – Jonathan
    Nov 21 '18 at 19:46











  • @Jonathan the intersection is the index, not the value. Perhaps I mislead you with the variable name. The intersected value is between data2(intersection_value-1) and data2(intersection_value)

    – Ander Biguri
    Nov 21 '18 at 23:15











  • Would be interesting to see the plot

    – kkuilla
    Nov 22 '18 at 14:00











  • @kkuilla the plot is almost the same of what OP gave, just with ensured bin overlap. The answer is just and index so not plotable.

    – Ander Biguri
    Nov 22 '18 at 14:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408138%2ffinding-where-two-histograms-cross-paths-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














From your example data, with a simple modification:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h1.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];

h2.Normalization = 'probability';
h2.BinWidth = 0.25;
h2.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];


data1=h1.Values;
data2=h2.Values;
intersection_value=find(data2>data1,1); % this is the index, bad variable name





share|improve this answer


























  • Hmm, this gives me a different answer than expected. When I ran this, I get an intersection value of ~15 everytime. However, the paths clearly cross [0,1]. I've updated my answer to make this more clear.

    – Jonathan
    Nov 21 '18 at 19:46











  • @Jonathan the intersection is the index, not the value. Perhaps I mislead you with the variable name. The intersected value is between data2(intersection_value-1) and data2(intersection_value)

    – Ander Biguri
    Nov 21 '18 at 23:15











  • Would be interesting to see the plot

    – kkuilla
    Nov 22 '18 at 14:00











  • @kkuilla the plot is almost the same of what OP gave, just with ensured bin overlap. The answer is just and index so not plotable.

    – Ander Biguri
    Nov 22 '18 at 14:37
















1














From your example data, with a simple modification:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h1.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];

h2.Normalization = 'probability';
h2.BinWidth = 0.25;
h2.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];


data1=h1.Values;
data2=h2.Values;
intersection_value=find(data2>data1,1); % this is the index, bad variable name





share|improve this answer


























  • Hmm, this gives me a different answer than expected. When I ran this, I get an intersection value of ~15 everytime. However, the paths clearly cross [0,1]. I've updated my answer to make this more clear.

    – Jonathan
    Nov 21 '18 at 19:46











  • @Jonathan the intersection is the index, not the value. Perhaps I mislead you with the variable name. The intersected value is between data2(intersection_value-1) and data2(intersection_value)

    – Ander Biguri
    Nov 21 '18 at 23:15











  • Would be interesting to see the plot

    – kkuilla
    Nov 22 '18 at 14:00











  • @kkuilla the plot is almost the same of what OP gave, just with ensured bin overlap. The answer is just and index so not plotable.

    – Ander Biguri
    Nov 22 '18 at 14:37














1












1








1







From your example data, with a simple modification:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h1.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];

h2.Normalization = 'probability';
h2.BinWidth = 0.25;
h2.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];


data1=h1.Values;
data2=h2.Values;
intersection_value=find(data2>data1,1); % this is the index, bad variable name





share|improve this answer















From your example data, with a simple modification:



x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h1.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];

h2.Normalization = 'probability';
h2.BinWidth = 0.25;
h2.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];


data1=h1.Values;
data2=h2.Values;
intersection_value=find(data2>data1,1); % this is the index, bad variable name






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 11:10

























answered Nov 21 '18 at 18:36









Ander BiguriAnder Biguri

26.2k105491




26.2k105491













  • Hmm, this gives me a different answer than expected. When I ran this, I get an intersection value of ~15 everytime. However, the paths clearly cross [0,1]. I've updated my answer to make this more clear.

    – Jonathan
    Nov 21 '18 at 19:46











  • @Jonathan the intersection is the index, not the value. Perhaps I mislead you with the variable name. The intersected value is between data2(intersection_value-1) and data2(intersection_value)

    – Ander Biguri
    Nov 21 '18 at 23:15











  • Would be interesting to see the plot

    – kkuilla
    Nov 22 '18 at 14:00











  • @kkuilla the plot is almost the same of what OP gave, just with ensured bin overlap. The answer is just and index so not plotable.

    – Ander Biguri
    Nov 22 '18 at 14:37



















  • Hmm, this gives me a different answer than expected. When I ran this, I get an intersection value of ~15 everytime. However, the paths clearly cross [0,1]. I've updated my answer to make this more clear.

    – Jonathan
    Nov 21 '18 at 19:46











  • @Jonathan the intersection is the index, not the value. Perhaps I mislead you with the variable name. The intersected value is between data2(intersection_value-1) and data2(intersection_value)

    – Ander Biguri
    Nov 21 '18 at 23:15











  • Would be interesting to see the plot

    – kkuilla
    Nov 22 '18 at 14:00











  • @kkuilla the plot is almost the same of what OP gave, just with ensured bin overlap. The answer is just and index so not plotable.

    – Ander Biguri
    Nov 22 '18 at 14:37

















Hmm, this gives me a different answer than expected. When I ran this, I get an intersection value of ~15 everytime. However, the paths clearly cross [0,1]. I've updated my answer to make this more clear.

– Jonathan
Nov 21 '18 at 19:46





Hmm, this gives me a different answer than expected. When I ran this, I get an intersection value of ~15 everytime. However, the paths clearly cross [0,1]. I've updated my answer to make this more clear.

– Jonathan
Nov 21 '18 at 19:46













@Jonathan the intersection is the index, not the value. Perhaps I mislead you with the variable name. The intersected value is between data2(intersection_value-1) and data2(intersection_value)

– Ander Biguri
Nov 21 '18 at 23:15





@Jonathan the intersection is the index, not the value. Perhaps I mislead you with the variable name. The intersected value is between data2(intersection_value-1) and data2(intersection_value)

– Ander Biguri
Nov 21 '18 at 23:15













Would be interesting to see the plot

– kkuilla
Nov 22 '18 at 14:00





Would be interesting to see the plot

– kkuilla
Nov 22 '18 at 14:00













@kkuilla the plot is almost the same of what OP gave, just with ensured bin overlap. The answer is just and index so not plotable.

– Ander Biguri
Nov 22 '18 at 14:37





@kkuilla the plot is almost the same of what OP gave, just with ensured bin overlap. The answer is just and index so not plotable.

– Ander Biguri
Nov 22 '18 at 14:37


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408138%2ffinding-where-two-histograms-cross-paths-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]