help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] problem of using 'gsl_stats_mean'


From: Kyunghoon (K) Lee
Subject: [Help-gsl] problem of using 'gsl_stats_mean'
Date: Sat, 24 Jun 2006 16:24:35 -0400

Hello all,

I'm having trouble with getting means of each column from a matrix.
I have a sample input file, which has the following data.

sample.dat:
2.5     2.4
0.5     0.7
2.2     2.9
1.9     2.2
3.1     3.0
2.3     2.7
2       1.6
1       1.1
1.5     1.6
1.1     0.9

My code reads the input into a gsl_matrix, then computes a mean of a column
after extracting a column form the gsl_matrix.
The thing is I'm getting some garbage values for the mean, which is supposed
to be 1.81.
It seems the column looks fine, and I couldn't figure it out on my own.
I'd appreciate it if someone would give me some advices to nail the bug.
Thank you in advance.

my code:
#include <stdio.h>
#include <string.h>
#include <gsl/gsl_matrix.h>

#define MAX_LINE_LENGTH 80

int get_num_column(FILE* file_ptr) {
   char line[MAX_LINE_LENGTH];
   char delimiters[] = " \f\n\r\t\v"; // standard whitespace characters
   char* ch_ptr;
   int i=0;  //counter

   fgets(line, sizeof(line), file_ptr);
   //printf("%s\n",line);
   ch_ptr = strtok(line, delimiters);
   while(ch_ptr != NULL) {
       //printf("%s\n",pch);
       ch_ptr = strtok(NULL, delimiters);
       i++;
   }
   //printf("%d\n",i);
   rewind(file_ptr); /* reset the file pointer's position */
   return i;
}

int get_num_row(FILE* file_ptr) {
   char line[MAX_LINE_LENGTH];
   int i=0;
   while (fgets(line, sizeof(line), file_ptr) != NULL) {
       //printf("%s\n",line);
       i++;
   }
   rewind(file_ptr); /* reset the file pointer's position */
   return i;
}

int main(void) {
   FILE* file_ptr;
   file_ptr=fopen("./sample.dat","r");

   if(file_ptr==NULL) {
       printf("Error: cannot open a file.\n");
       /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
       return 1;
   }

   //***** get the size of data
   const int num_column = get_num_column(file_ptr);
   const int num_row    = get_num_row(file_ptr);

   printf("Num of Columns = %d\n",get_num_column(file_ptr));
   printf("Num of Rows    = %d\n",get_num_row(file_ptr));

   gsl_matrix* m = gsl_matrix_calloc(num_row, num_column);
   gsl_matrix_fscanf(file_ptr, m);

   // check out loaded data
   int i,j;
   for (j = 0; j < num_column; j++) {
       printf("%i th column:\n", j);
       for (i = 0; i < num_row; i++) {
           printf("%g\n", gsl_matrix_get(m, i, j));
       }
       printf("\n");
   }

   //***** read data
   //get_data(file_ptr,m);


   //**** mean
   gsl_vector* column = gsl_vector_calloc(num_row);
   gsl_matrix_get_col(column, m, 0);
   // check out loaded data
   for (i = 0; i < num_row; i++) {
       printf("%g\n", gsl_vector_get(column, i));
   }
   printf("\n");
   for (i = 0; i < num_row; i++) {
       printf("%g\n", column->data[i]);
   }
   //double gsl_stats_mean (const double data[], size_t stride, size_t n)
   double mean = gsl_stats_mean(column->data, 1, num_row);
   printf("mean = %g\n", mean);

   gsl_vector_free(column);
   gsl_matrix_free(m);
   return 0;
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]