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 7173057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:44:39+00:00 2026-05-28T15:44:39+00:00

I want to store in an array a symmetric matrix for a matrix I

  • 0

I want to store in an array a symmetric matrix

for a matrix I was doing this

    double[,] mat = new double[size,size];
    for (int i = 0; i < size; i++)
    {
      for (int j = 0; j <= i; j++)
           mat[i, j] = mat[j, i] = (n * other_matrix[i,j]);
    }

If I want to store in an array

double[] mat = new double[size*size];

instead of

 double[,] mat

What would be the most efficient way?

using mat[i*n+j]?

  • 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-28T15:44:40+00:00Added an answer on May 28, 2026 at 3:44 pm

    Yes.

    Store the elements by row, where the i-th row and j-th column is stored in index k=i*NC+j with NC the number of columns. This applies to a non-symmetric general matrix.

    To store a symmetric matrix of size N you only need N*(N+1)/2 elements in the array. You can assume that i<=j such that the array indexes go like this:

    k(i,j) = i*N-i*(i+1)/2+j            i<=j  //above the diagonal
    k(i,j) = j*N-j*(j+1)/2+i            i>j   //below the diagonal
    

    with

    i = 0 .. N-1
    j = 0 .. N-1
    

    Example when N=5, the array indexes go like this

    | 0   1   2   3   4 |
    |                   |
    | 1   5   6   7   8 |
    |                   |
    | 2   6   9  10  11 |
    |                   |
    | 3   7  10  12  13 |
    |                   |
    | 4   8  11  13  14 |
    

    The total elements needed are 5*(5+1)/2 = 15 and thus the indexes go from 0..14. Check

    The i-th diagonal has index k(i,i) = i*(N+1)-i*(i+1)/2. So the 3rd row (i=2) has diagonal index k(2,2) = 2*(5+1)-2*(2+1)/2 = 9. Check

    The last element of the i-th row has index = k(i,N) = N*(i+1)-i*(i+1)/2-1. So the last element of the 3rd row is k(2,4) = 5*(2+1)-2*(2+1)/2-1 = 11. Check

    The last part that you might need is how to go from the array index k to the row i and column j. Again assuming that i<=j (above the diagonal) the answer is

    i(k) = (int)Math.Floor(N+0.5-Math.Sqrt(N*(N+1)-2*k+0.25))
    j(k) = k + i*(i+1)/2-N*i
    

    To check the above I run this for N=5, k=0..14 and got the following results:

    Table of indexes

    Which is correct! Check

    To make the copy then just use Array.Copy() on the elements which is super fast. Also to do operations such as addition and scaling you just need to work on the reduced elements in the array, and not on the full N*N matrix. Matrix multiplication is a little tricky, but doable. Maybe you can ask another question for this if you want.

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

Sidebar

Related Questions

I want to store an array of UIImage and I do this: //in didFinishLaunchingWithOption
I want to store array into mysql db something like this item_row = nike,adidas,puma
I am new to Java, I want to store an array of pair of
I want to store the array returned by a method into another array. How
I just want to quickly store an array which I get from a remote
Google App Engine - When I want to store a byte array as one
I want to store several different methods in an array in Ruby. Suppose I
I want to store my CGPoint to the NSMutable Array, so , I have
I want to store few strings into an array using ArrayList. How can I
In my ASP.NET web application, I want to store an array of data that

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.