Search code examples
matlabformattingsparse-matrix

Why does my data save in this format in MatLab?


I have data saved from each loop run and it looks like this.

val =

   (1,1)      16.0000

   (9,1)       0.0017

  (10,1)       0.0040

  (11,1)       0.0049

  (12,1)       0.0048

  (13,1)       0.0043

The matrix is 50x50, however only the cells that contain the specific element are showed in this save.

I know that those are the values and their corresponding locations.

I want to know what this format is and how can I count the the number cells shown this way quickly.


Solution

  • This is a sparse matrix, as @Oli already said correctly. I will try to elaborate a little bit.

    You create it with the following commands:

     s = sparse(zeros(13,13));
     s(1,1) = 16;
     s(9,1) = 0.0017;
     s(10,1) = 0.0040;
     ...
    

    Convert it back to full matrix by using full command:

     f = full(s);
    

    And it you want to count the number of entries, use nnz. It counts the number of non zero entries:

     z = nnz(s);