Removing a column from a gsl_matrix

This version works:

#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>

int remove_col (int K,
                int N,
                int i,//between range 1 to N
                gsl_matrix *Sn, //Kx(N-1)
                gsl_matrix *Z) {
  gsl_matrix_view Z_view;

  gsl_matrix_view Sn_view;
  if (i==0){
    Z_view  = gsl_matrix_submatrix (Z, 0, 1, K, N-1);
    gsl_matrix_memcpy (Sn, &Z_view.matrix);
  } else{
    Z_view  = gsl_matrix_submatrix (Z, 0, 0, K, i);
    Sn_view = gsl_matrix_submatrix (Sn, 0, 0, K, i);
    gsl_matrix_memcpy (&Sn_view.matrix, &Z_view.matrix);
    Z_view  = gsl_matrix_submatrix (Z, 0, i, K, N-i);
    Sn_view = gsl_matrix_submatrix (Sn, 0, i-1, K, N-i);
    gsl_matrix_memcpy (&Sn_view.matrix, &Z_view.matrix);
  }     
}  
int main () {
  double Zx[]={1, 1, 1, 1, 1, 1,
               0, 1, 1, 1, 1, 1,
               0, 0, 1, 1, 1, 1,
               0, 1, 0, 1, 1, 1,
               1, 1, 0, 1, 1, 0,
               1, 1, 1, 1, 1, 1,
               1, 1, 0, 1, 0, 0,
               1, 1, 1, 0, 1, 0,
               1, 1, 0, 1, 0, 0,
               0, 1, 1, 1, 0, 1};

  int r=10;             
  int c=6; 
  gsl_matrix_view Z = gsl_matrix_view_array (Zx, r, c); 

  gsl_matrix *R= gsl_matrix_calloc(c,r);
  for ( int row = 0; row < r; ++row ) { 
    for ( int col = 0; col < c; ++col ) { 
      gsl_matrix_set( R, col, row, gsl_matrix_get( &Z.matrix , row, col));
    }
  }
  for (int row=0; row<r; ++row){
    for (int col=0; col<c; ++col)
      printf(col==r-2?"%6.3f\n":"%6.3f ",gsl_matrix_get(R,col,row));
    printf("\n");
  }

  gsl_matrix *Sn = gsl_matrix_alloc(c,r-1);
  remove_col (c,r,1, Sn, R);
  for (int row=0; row<r-1; ++row){
    for (int col=0; col<c; ++col)
      printf(col==r-2?"%6.3f\n":"%6.3f ",gsl_matrix_get(Sn,col,row));
    printf("\n");
  }
  gsl_matrix_free(R);    
  gsl_matrix_free(Sn);    

  return 0;
}

The right way to remove a column from a gsl_matrix is:

int remove_col (int K,
                int N,
                int i,//between range 1 to N
                gsl_matrix *Sn, //Kx(N-1)
                gsl_matrix *Z)
{
    //remove a row of matrix
    int j;
    gsl_matrix_view Z_view;

    gsl_matrix_view Sn_view;
    if (i==0){
       Z_view  = gsl_matrix_submatrix (Z, 0, 1, K, N-1);
       gsl_matrix_memcpy (Sn, &Z_view.matrix);
     }
    else if(i==(N-1)){
       Z_view  = gsl_matrix_submatrix (Z, 0, 0, K, N-1);
       gsl_matrix_memcpy (Sn, &Z_view.matrix);
    }else{
       j=i+1;
       Z_view  = gsl_matrix_submatrix (Z, 0, 0, K, j);
       Sn_view = gsl_matrix_submatrix (Sn, 0, 0, K, j);
       gsl_matrix_memcpy (&Sn_view.matrix, &Z_view.matrix);
       Z_view  = gsl_matrix_submatrix (Z, 0, j, K, N-j);
       Sn_view = gsl_matrix_submatrix (Sn, 0, j-1, K, N-j);
       gsl_matrix_memcpy (&Sn_view.matrix, &Z_view.matrix);
    }
    return 0;
}

Tags:

C++

Gsl