Changed the init_flag function

The init_flag was not returning the proper values because of some kind
of pointer manipulation so now we are using a temporary field.
Changed write_matrix and create write_imatrix

For debugging I fixed the write_matrix and created write_imatrix
functions and they are working correctly