# Read/Write data with rhdf5

,

## The inconsistency

### Original matrix `a`

### Matrix created and written with `rhdf5`

The matrix written to a data set created by `rhdf5`

This matrix when shown in HDFView looks like this

### Matrix created in HDFView and filled by `rhdf5`

The matrix written to a data set created by HDFView, with `Dimensions = 5 x 8`

And read into R

## Cause and Fix

The cause of such inconsistency is in the way `h5write`

and `h5read`

works with HDF5 file. Any flattening of nD array is done by-row in HDFView, and by-column in R. So when `h5write`

is called, the input matrix is first **flattened by column** in R, and then write to the file **by row**. Conversely, when `h5read`

is called, the data set is **read by row**, and R **represent it by col**. Essentially any reading operation into R will transpose the matrix stored in the file (as seen in HDFView). So to get back the original, we should **always** transpose the data set after reading.

In order to preserve the input matrix when writing into HDF5 file, we need to transpose the matrix before writing, so it will show up correctly in C-program (like HDFView)

And then transpose it again after reading it into R to get back the original dimension