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

  • Home
  • SEARCH
  • 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 986457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:19:24+00:00 2026-05-16T05:19:24+00:00

I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat

  • 0

I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat random, and I can’t count on all the values being a set distance away from the diagonal.

However, it is still sparse, and I want to reduce the storage requirement on the matrix. Therefore, I am trying to figure out how to store each row starting at the first non-zero, in order, until I get to the last non-zero.

That is, if the first non-zero of row m occurs at column 2, and the last non-zero is at column 89, I want to store in A[m] rows 2-> 89.

Since each row does not have the same number of non-zeros, I will make all the rows of A have the same number of elements, and pad zeros to the end of the row for rows that have a smaller number of non-zero elements.

How do I do this translation in C? I do not actually have an original, full matrix to just copy the values from (the original matrix is coming to me in CSR form). If I was doing this in fortran, I could just define my array to be two dimensional and just have each row be variable length by tracking the start/stop values of non-zero columns and store it like that.

I will try to demonstrate below:

This is a matrix representation of the values I know – and for each value, I know the row and column location

  [1    2    3    4                   ]
  [   5    6    7    8                ]
  [       10    11    12    13        ]
 m[   14    15    16    17       18   ]
  [         19    20    21         22 ]

Now for this one row m has the largest “span” between the first non-zero and last non-zero
so my new matrix is going to be 5x[span of row m]

  [1     2     3     4          ]
  [5     6     7     8          ]
  [10    11    12    13         ]
 m[14    15    16    17       18]
  [19    20    21       22      ] 

As you can see, row m needs no zero padding since it was the longest “span” anyway

The other rows now all have row zero as the first non-zero, and maintain the spacing of zeros columns between each non-zero.

  • 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-16T05:19:24+00:00Added an answer on May 16, 2026 at 5:19 am

    I would implement this as a ragged array, with A[n][0] always returning the element on the diagonal. A[n][1] will return the item just to the right of the diagonal, A[n][2] will return the item to the left of the diagonal, and so. Then, you just need a function that maps matrix index [i,j] to ragged array index[r][s].

    This has the advantage of sparsity, and if your values stay close to the diagonal the arrays are not very long.


    Alternatively, you could have this definition:

    struct Row
    {
        int InitialOffset;
        int NumElements;
        int[] Values;
    }
    

    Then you would have a Row[]. Retrieving a value based on matrix index would look like this:

    //matrix is merely an array of rows...
    int GetValue(*matrix this, int i, int j)
    {
        Row CurrentRow = (*this)[i];
        if (CurrentRow.InitialOffset > j)
            return 0;
        else if (CurrentRow.InitialOffset + CurrentRow.NumElements < j)
            return 0; 
        return CurrentRow.Values[j - CurrentRow.InitialOffset]
    }
    

    My C syntax is a little hazy, but you should get the idea.


    Based on your demonstration, I would recommend this:

    struct Matrix
    {
        int[,] Data
        int[] StartOffset;
        int[] NumberElements;
    }
    

    Used as follows…

    int GetValue(*Matrix this, int i, int j)
    {
        if (this.StartOffset[i] > j)
            return 0;
        else if (this.StartOffset[i] + this.NumberElements[i] < j)
            return 0; 
        return this.Data[i, j-this.StartOffset[i]];
    }
    

    Your initialization procedure would look something like this

    //Data is a struct that holds row index, col index, and value
    Matrix* InitMatrix (*Data values, int numVals)
    {
        //loop through values to find longest row and number of rows
        //create new matrix, malloc matrix for longrow * numRows
        //malloc numrows elements for StartOffset and NumItems
        //foreach row, find min() and max()-min() of col indexs and 
        //store in StartOffset and NumItems
    }
    

    You need to do some processing, but data compression isn’t cheap.

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

Sidebar

Related Questions

I have a 30000x14000 sparse matrix in MATLAB (version 7), which I need to
I have a sparse array in Jscript, with non-null elements occuring at both negative
I have a lot of spare intel linux servers laying around (hundreds) and want
For programming in my spare time, I have both a desktop and a laptop
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you determined a maximum number of characters allowed in FCKEditor ? I seem

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.