Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: C: deleting row and column from matrix

  1. #1
    Join Date
    Dec 2009
    Beans
    302

    C: deleting row and column from matrix

    Hi,

    I suppose to have the following matrix A) and I want to remove a generic row and column.
    For instance the second row and column, then I get B). How can I realize it in C? I cannot figure out how to do.

    A)
    a00 a01 a02
    a10 a11 a12
    a20 a21 a22
    B)
    a00 a02
    a20 a22

    Thank you

  2. #2
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: C: deleting row and column from matrix

    I hope you're not using a double-pointer "matrix", because that makes everything harder.

    1) If you're allowed to use a library, take a look at the GNU Scientific Library. http://www.gnu.org/software/gsl/manu.../Matrices.html
    2) If not, I'd go for a data structure that implements the matrix in C-order, i.e. using a flat structure, so that elemet (row, column) = row * row_size + column.

  3. #3
    Join Date
    Dec 2009
    Beans
    302

    Re: C: deleting row and column from matrix

    Hi,

    I'm using an array in particular a pointer to it. Yes, I'm using the flat structure (row, column) = row * row_size + column and the size of matrix is fixed (preallocated at the beginning in the main). I cannot figure out an algorithm that allows to me to remove a generic row-column from my matrix. As in the example reported above, I have
    A)
    a00 a01 a02
    a10 a11 a12
    a20 a21 a22
    B)
    a00 a02
    a20 a22

    They are represented in my flat structure as a00 a01 a02 | a10 a11 a12 | a20 a21 a22 -> a00 a02 0 | a20 a22 0 | 0 0 0.
    Any suggestion is appreciated.
    Thank

  4. #4
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: C: deleting row and column from matrix

    Observe the following: if you're deleting column x, then you're deleting everything that is x modulo row_size in your linear order; if you're deleting row x, you're deleting everything that is between row_size * x and row_size * (x + 1) in your linear order.

  5. #5
    Join Date
    Dec 2009
    Beans
    302

    Re: C: deleting row and column from matrix

    Quote Originally Posted by nvteighen View Post
    Observe the following: if you're deleting column x, then you're deleting everything that is x modulo row_size in your linear order; if you're deleting row x, you're deleting everything that is between row_size * x and row_size * (x + 1) in your linear order.
    Ok you are right, but I have also to move the element from the rows and columns that have greater indexes w.r.t. the row-column that I'm removing. For instance if you consider the same example in which the first row and column are removed, the others elements have to be moved
    A)
    a00 a01 a02
    a10 a11 a12
    a20 a21 a22
    B)
    a11 a12
    a21 a22
    and the linear representation is a00 a01 a02 | a10 a11 a12 | a20 a21 a22 -> a11 a12 0 | a21 a22 0 | 0 0 0. So it seems hard to do. I have not brilliant idea.
    Thank

  6. #6
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: C: deleting row and column from matrix

    Why move them? Return a new matrix and leave the original untouched

  7. #7
    Join Date
    Dec 2009
    Beans
    302

    Re: C: deleting row and column from matrix

    Ok, if I can copy the matrix with deleted row and column in other matrix it's straightforward. Is there the possibility to do it in place without allocating another matrix?

    Thank

  8. #8
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C: deleting row and column from matrix

    the following code (extremely basic, to be honest) does what you are looking for, but it needs to be be strongly improved for becoming an acceptable piece of software (I'm sorry but I'm not a C programmer)
    Code:
    #include <stdio.h>
    
    #define DELETED_ROW 1
    #define DELETED_COLUMN 1
    
    
    int main ()
    {
        int A[3][3], B[2][2];
        int i, j;
        for (i = 0; i < 3; i++)
            for (j = 0; j < 3; j++)
                A[i][j] = i * 3 + j + 1;
        for (i = 0; i < 3; i++)
        {
            printf("\n");
            for (j = 0; j < 3; j++)
                printf("A[%d][%d]=%d\t", i, j, A[i][j]);
        }
        for (i = 0; i < 3; i++)
            if (i != DELETED_ROW)
            {
                for (j = 0; j < 3; j++)
                    if (j != DELETED_COLUMN)
                    {
                        if (i < DELETED_ROW && j < DELETED_COLUMN)
                            B[i][j] = A[i][j];
                        else if (i < DELETED_ROW && j > DELETED_COLUMN)
                            B[i][j-1] = A[i][j];
                        else if (i > DELETED_ROW && j < DELETED_COLUMN)
                            B[i-1][j]=A[i][j];
                        else
                            B[i-1][j-1]=A[i][j];
                    }
            }
        for (i = 0; i < 2; i++)
        {
            printf("\n");
            for (j = 0; j < 2; j++)
                printf("B[%d][%d]=%d\t", i, j, B[i][j]);
        }
        return 0;
    }

  9. #9
    Join Date
    Dec 2009
    Beans
    302

    Re: C: deleting row and column from matrix

    Quote Originally Posted by alan9800 View Post
    the following code (extremely basic, to be honest) does what you are looking for, but it needs to be be strongly improved for becoming an acceptable piece of software (I'm sorry but I'm not a C programmer)
    Code:
    #include <stdio.h>
    
    #define DELETED_ROW 1
    #define DELETED_COLUMN 1
    
    
    int main ()
    {
        int A[3][3], B[2][2];
        int i, j;
        for (i = 0; i < 3; i++)
            for (j = 0; j < 3; j++)
                A[i][j] = i * 3 + j + 1;
        for (i = 0; i < 3; i++)
        {
            printf("\n");
            for (j = 0; j < 3; j++)
                printf("A[%d][%d]=%d\t", i, j, A[i][j]);
        }
        for (i = 0; i < 3; i++)
            if (i != DELETED_ROW)
            {
                for (j = 0; j < 3; j++)
                    if (j != DELETED_COLUMN)
                    {
                        if (i < DELETED_ROW && j < DELETED_COLUMN)
                            B[i][j] = A[i][j];
                        else if (i < DELETED_ROW && j > DELETED_COLUMN)
                            B[i][j-1] = A[i][j];
                        else if (i > DELETED_ROW && j < DELETED_COLUMN)
                            B[i-1][j]=A[i][j];
                        else
                            B[i-1][j-1]=A[i][j];
                    }
            }
        for (i = 0; i < 2; i++)
        {
            printf("\n");
            for (j = 0; j < 2; j++)
                printf("B[%d][%d]=%d\t", i, j, B[i][j]);
        }
        return 0;
    }
    Hi,
    your solution works but requires two matrices while I'm searching a solution that works directly with the same matrix. Moreover, the matrix is stored as a single array.
    Thank anyway

  10. #10

    Re: C: deleting row and column from matrix

    You need to put in some effort mate. alan was kind enough to give you an almost ready-made solution that just needs a little bit of tweaking. Don't expect people to do your homework for you; take a stab at it, then post the code here when you get stuck.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •