more mconvert
[pspp] / src / language / data-io / matrix-reader.c
index c2d06a071dac88cf276657b63591c1321ed2cb17..44ea5b18a88c8929011a4e37947a99725bf128b5 100644 (file)
@@ -259,11 +259,7 @@ matrix_reader_next (struct matrix_material *mm, struct matrix_reader *mr,
   const struct variable **vars = mr->cvars;
   size_t n_vars = mr->n_cvars;
 
-  *mm = (struct matrix_material) {
-    .n = gsl_matrix_calloc (n_vars, n_vars),
-    .mean_matrix = gsl_matrix_calloc (n_vars, n_vars),
-    .var_matrix = gsl_matrix_calloc (n_vars, n_vars),
-  };
+  *mm = (struct matrix_material) { .n = NULL };
 
   struct matrix
     {
@@ -283,20 +279,23 @@ matrix_reader_next (struct matrix_material *mm, struct matrix_reader *mr,
     {
       struct substring rowtype = matrix_reader_get_string (c, mr->rowtype);
 
-      gsl_matrix *v
-        = (ss_equals_case (rowtype, ss_cstr ("N")) ? mm->n
-           : ss_equals_case (rowtype, ss_cstr ("MEAN")) ? mm->mean_matrix
-           : ss_equals_case (rowtype, ss_cstr ("STDDEV")) ? mm->var_matrix
+      gsl_matrix **v
+        = (ss_equals_case (rowtype, ss_cstr ("N")) ? &mm->n
+           : ss_equals_case (rowtype, ss_cstr ("MEAN")) ? &mm->mean_matrix
+           : ss_equals_case (rowtype, ss_cstr ("STDDEV")) ? &mm->var_matrix
            : NULL);
       if (v)
         {
+          if (!*v)
+            *v = gsl_matrix_calloc (n_vars, n_vars);
+
           for (int x = 0; x < n_vars; ++x)
             {
               double n = case_num (c, vars[x]);
-              if (v == mm->var_matrix)
+              if (v == &mm->var_matrix)
                 n *= n;
               for (int y = 0; y < n_vars; ++y)
-                gsl_matrix_set (v, y, x, n);
+                gsl_matrix_set (*v, y, x, n);
             }
           continue;
         }