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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:40:58+00:00 2026-05-15T18:40:58+00:00

I’m trying to create a matrix data structure in C. I have a struct

  • 0

I’m trying to create a matrix data structure in C. I have a struct and have a two dimensional void pointer array (size is dynamically defined at the heap) for the cargo part(data) in this struct.

Given a column index, I want to get the values of this column in a one dimensional array. It is easy to this with one for or while loop. But if the number of rows in this matrix is N, then it’ll take O(N) time for getting a column vector. Can I do this more efficiently with memory operations like memcpy and how? Otherwise how can I improve the performance(My data is pretty structured and I need to store this in some kind of matrix).

  • 1 1 Answer
  • 1 View
  • 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-15T18:40:59+00:00Added an answer on May 15, 2026 at 6:40 pm

    If you want to copy the data in your matrix, you can’t do it in less than O(N) time whether it is a row or a column, except for small N where hardware features might help.

    However, if your matrices are immutable, you can use smoke and mirrors to give the illusion of having a separate column vector.

    The below code is typed straight in to the answer text box and has not even been compiled. Use at your own risk!

    Your matrix type is defined as a struct thus:

    typedef struct 
    {
        unsigned int refCount;  // how many Matrixes are referencing this data ref
        size_t lineWidth;       // number of doubles between element at row = n, col = 0 and row = n +1, col = 0 
        double* data;           // the actual data
    } DataRef;
    
    typedef struct
    {
        size_t rows;            // num rows in matrix
        size_t cols;            // num cols in matrix
        size_t dataOffset;      // offset in doubles from the start of data of element at row = 0, col = 0
        DataRef* data;
    } Matrix;
    

    To create a brand new matrix (I’ve left out all the error handling to make it simpler).

    Matrix* matrix_create(size_t rows, size_t cols, const double* values)
    {
        Matrix* ret = calloc(1, sizeof *ret);
        ret->rows = rows;
        ret->cols = cols;
        ret->dataOffset = 0;
        ret->data = calloc(1, sizeof *dataRef);
        ret->data->lineWidth = cols;
        ret->data->data = allocateAndCopy(rows * cols, values); // mallocs a new block of doubles big enough for the values
        ret->data->refCount = 1;
        return ret;
    }
    

    To access an element (again no error handling, eg bounds errors)

    double matrix_elementAt(Matrix* matrix, size_t row, size_t col)
    {
        size_t offset = matrix->dataOffset + row * matrix->data->lineWidth + col;
        return *(matrix->data->data + offset);
    }
    

    To create a new matrix from a rectangular region of another matrix (again, error handling needed)

    Matrix* matrix_createFromRegion(Matrix* old, size_t startRow, size_t startCol, size_t rows, size_t cols)
    {
        Matrix* ret = calloc(1, sizeof *ret);
        ret->rows = rows;
        ret->cols = cols;
        ret->dataOffset = old->dataOffset + startRow * old->dataLineWidth + startCol;
        ret->data = old->data;
        ret->data->refCount++;
        return ret;
    }
    

    To create a new matrix from a column in another matrix:

    Matrix* vector = matrix_createFromRegion(aMatrix, 0, colYouWant, matrix_numRows(aMatrix), 1);
    

    To free a matrix

    void matrix_free(Matrix* aMatrix)
    {
        if (aMatrix->data->refCount == 1)
        {
            free(aMatrix->data->data);
            free(aMatrix->data);
        }
        else
        {
            aMatrix->data->refCount--;
        }
        free(aMatrix);
    }
    

    If you want mutable matrices, any time you modify an element, check the refCount and if it is greater than 1, copy the DataRef before modifying it (decrement the refCount on the old dataRef), otherwise modify the dataRef in place.

    Now the above uses lots of mallocs and so might be less efficient than the naive implementation for small matrices. However, you could maintain a list of unused DataRef structs and Matrix structs and instead of freeing them when you are done, put them on the free list. When allocating new ones, get the structs from the free lists unless they are empty. That way, getting a matrix that represents a column of an existing matrix will often take constant time.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.