Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5935599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:16:40+00:00 2026-05-22T15:16:40+00:00

How to create a 2d sparse matrix in a MEX-file written in C. After

  • 0

How to create a 2d sparse matrix in a MEX-file written in C. After creating the matrix how to access the elements individually like in C , say mat[i][j]?

I tired using mxCreateNumericArray function but I wasn’t able to access the elements and also make it as a sparse matrix.

Please help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:16:41+00:00Added an answer on May 22, 2026 at 3:16 pm

    See this page on mxCreateSparse. Then you’ll want to look at mxSetPr, mxSetIr and mxSetJc and the corresponding “get” versions.

    Here’s an example of how to allocate a sparse matrix. I realize this is an old link, but to the best of my knowledge, it hasn’t changed.

    Basically, how it works is that the ir data contains the row indices. The jr data contains a list of indices into the ir array. For instance, in the link on how to allocate 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 array static_jc_data tells you that indices static_jc_data[c] through static_jc_data[c+1]-1 of static_pr_data and static_ir_data correspond to the column c of the matrix. Within that range (static_jc_data[c] to static_jc_data[c+1]-1) the entries of static_pr_data gives you the values in the matrix and static_ir_data gives you the correct rows.

    For example, the matrix here would be:

    A = [ 5.8  0
          0    5.9
          6.2  0
          0    6.1];
    

    To answer your questions about how to access elements individually, you have to search for whether the i,jth element exists and if it does return it, otherwise return 0. To do this, you’d search from static_ir_data[static_jc_data[j]] through static_ir_data[static_jc_data[j+1]-1] to see whether your i exists. If it does, then the corresponding entry in static_pr_data will contain your entry. If it doesn’t, then return 0.

    However, typically with sparse matrix usage, if you’re doing a lot of searching through the matrix to see if a certain element exists, you may want to think about how you’re using it. Typically, it’s much better to perform whatever operation you’re doing by only going through the non-zero elements once instead of searching for each i,jth entry.

    Oh, and one last thing. Keep in mind that in the MEX code, all your indices are 0 based, but they are 1 based in MATLAB. That should add to the fun.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to create a sparse file such that all-zero blocks don't take up
I'm trying to create a sparse vector class in C++, like so: template<typename V,
I have created a sparse matrix using MEX and also created a sparse matrix
I'm reading a sparse table from a file which looks like: 1 0 7
I can create a sparse php array (or map) using the command: $myarray =
I wrote a sparse vector class (see #1 , #2 .) I would like
I'm doing a main.cpp to test my implementation of a sparse matrix, where I
I have made a List of Lists implementation of a sparse matrix, and I've
Is there any way to access the number of blocks allocated to a file
I'm working on a sparse matrix class that needs to use an array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.