Niner710 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am using Math::GSL::Histogram to do some data crunching. I want to take a group of numbers.
For example.
40 55 85 48 32

Bin Range 0-40 Bin Range 41-60 Bin Range 61 - 100
And I want to determine how many numbers in the list are in a certain bin range and the % distribution. Does anyone know how to do this with the Math::GSL::histogram package? I looked at the documentation but am confused as to what functions I need to do this.

Replies are listed 'Best First'.
Re: How do I make a histogram Question?
by zentara (Cardinal) on Jan 27, 2009 at 16:16 UTC
Re: How do I make a histogram Question?
by JadeNB (Chaplain) on Jan 27, 2009 at 20:58 UTC
    It seems from the documentation that
    my $h = gsl_histogram_alloc 3; gsl_histogram_set_ranges $h, [0, 41, 61, 100], 4; gsl_histogram_increment $h, $_ for @values;
    will do what you like, whereupon the histogram can be printed using gsl_histogram_fprintf or gsl_histogram_fscanf. (I don't understand why gsl_histogram_set_ranges needs that final argument, since it can be figured automatically once $h is known; but the documentation says it does, and I am not in a position to test.)

    (Boy, gotta love those flat namespaces. I'll bet it's fun to call these functions as Math::GSL::Histogram::gsl_histogram_xxx.)

      Ok, thanks for the help. I was trying to use your code and do a

      gsl_histogram_fscanf($stream, $h)

      What is the $stream? I read it has been opened by the gsl_fopen_function from the Math::GSL module. Does that mean it is already predefined and I don't have to define it myself. I get a compile error.
        The simple answer is that you need first to run
        my $stream = Math::GSL::gsl_fopen $file, $mode;
        The complicated answer is that gsl_fopen is defined in Math::GSL, which uses the fopen function from Math::GSL::Vector (for some reason), which is an alias of Math::GSL::Vectorc::fopen, whose definition I can't find—so I can't tell you anything but the obvious about $file, and nothing at all about $mode, except that it can end in +b.

        For future reference, it would have been perfectly OK to continue our discussion in the Chatterbox.