See this page at mxCreateSparse. Then you will want to look at mxSetPr , mxSetIr and mxSetJc and the corresponding βgetβ versions.
Here is an example of how to distribute a sparse matrix. I understand that this is an old link, but as far as I know, it has not changed.
Basically, how this works is that the ir data contains row indices. The jr data contains a list of indices in the ir array. For example, in the link on how to distribute a sparse matrix, the code:
... static double static_pr_data[NZMAX] = {5.8, 6.2, 5.9, 6.1}; static int static_ir_data[NZMAX] = {0, 2, 1, 3}; static int static_jc_data[COLS+1] = {0, 2, 4}; ...
the static_jc_data array tells you that the static_jc_data[c] indices through static_jc_data[c+1]-1 from static_pr_data and static_ir_data correspond to the column c matrix. Within this range (from static_jc_data[c] to static_jc_data[c+1]-1 ), static_pr_data entries give values ββin the matrix, and static_ir_data gives the correct rows.
For example, the following matrix would be here:
A = [ 5.8 0 0 5.9 6.2 0 0 6.1];
To answer your questions about how to access the elements individually, you need to look if i,j th exists, and if it returns, otherwise returns 0. To do this, you would look from static_ir_data[static_jc_data[j]] via static_ir_data[static_jc_data[j+1]-1] to see if your i exists. If so, then the corresponding entry in static_pr_data will contain your entry. If not, return 0.
However, as a rule, when using a sparse matrix, if you are much versed in the matrix to find out if any element exists, you might think about how you use it. As a rule, it is much better to perform any operation that you do, only once through non-zero elements instead of searching for every record i,j th.
Oh, and the last. Keep in mind that in MEX code, all your indexes are based on 0, but they are based on MATLAB. This should add to the fun.