Marco Grassi
Marco Grassi

Reputation: 89

How to convert a matrix saved as a struct to matrix

I downloaded a Matrix from https://sparse.tamu.edu/Embree/ifiss_mat, after loading this file i noticed that the matrix is stored as a struct containing 3 arrays of row indices, column indices and data. In this particular case represented as a struct containing Problem.A.ir, Problem.A.jc, Problem.A.data.

I am only used to having the matrix readily available as Problem.A, is there a way to convert from this struct to a 'normal' matrix?

Upvotes: 1

Views: 159

Answers (1)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22215

Having looked at the data and the description in the link you gave, it appears to me that this particular dataset follows the following pattern:

  • data are the values at the nonzero locations
  • ir are row indices IN ZERO-BASED FORM, corresponding to nonzero locations (same size as data)
  • jc is a helper vector telling you IN ZERO-BASED FORM when one column of row-indices stops and the next one starts in the ir vector

So, e.g. the first 20 entries in ir are:

0  1  2  33  34  35  66  67  68  0  1  2  33  34  35  66  67  68  0  1

The first 3 entries in jc are:

0  9  18

This means that in the first column, assuming ZERO-INDEXING, rows 0 1 2 33 34 35 66 67 68 (i.e. ir[0:8] have nonzero data (taken from data[0:8] respectively).

Then, in the second column, rows 0 1 2 33 34 35 66 67 68 (i.e. ir[9:17]) have nonzero data (taken from data[9:17] respectively.

Then you have to adjust all that by 1, to convert it to the ONE-BASED-INDEXING supported by matlab/octave.

Once you've processed all of this, you'll be able to create an array of i,j,v triplets corresponding to row, column, and value, for all the nonzero values in your dataset (i.e. the 3,599,932 entries in data). Then, you can pass these i, j, and v vectors into sparse, along with the dimensions of the matrix (96,307 x 96,307 according to the website), to create your sparse matrix.

Unfortunately I don't think there is a specific function that recognises this format and produces the matlab/octave equivalent sparse matrix for you. You'll have to come up with code to do this manually. This shouldn't be too hard though.


PS. Note that many of the other fields in the problem struct are in fact text, but for some reason it was encoded as ascii numbers. To retrieve the actual text you need to pass it into char, e.g. char(Problem.notes)

Upvotes: 2

Related Questions