Change column in matrix

This is a discussion on Change column in matrix within the C Programming forums, part of the General Programming Boards category; Hello, I got matrix 3x3 Code: double matrix[3][3]={{0}, {0}, {0}}; double vector[3]={7,7,7}; how to put the vector on the first ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Change column in matrix

    Hello,

    I got matrix 3x3
    Code:
    double matrix[3][3]={{0}, {0}, {0}};
    double vector[3]={7,7,7};
    how to put the vector on the first column of my matrix?
    do I understand correctly that matrix[0] is an adress to the first column?
    i would like to change the adress of first column to the adress of the vector.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Quote Originally Posted by plasticstone View Post
    Hello,

    I got matrix 3x3
    Code:
    double matrix[3][3]={{0}, {0}, {0}};
    double vector[3]={7,7,7};
    how to put the vector on the first column of my matrix?
    Use a for loop and assign every element of vector to the corresponding elements of matrix:
    Code:
    matrix[0][i] = vector[i];
    do I understand correctly that matrix[0] is an adress to the first column?
    Yes, that's correct, but arrays are not modifiable values. matrix[0] is an array of 3 doubles (remember, it's a 2-d array). Thus you can't change where matrix[0] points.

    i would like to change the adress of first column to the adress of the vector.
    Like I said, you can't change the address of the first column. You would need a 1-d array of pointers (double *matrix[3]; ) to do that, but it's not a very good approach. It will become very difficult to keep track of what's in matrix.
    Last edited by anduril462; 10-22-2011 at 10:21 AM.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by plasticstone View Post
    Hello,
    how to put the vector on the first column of my matrix?
    It depends on how do you interpret rows and columns but:
    Code:
    int i;
    
    for (i = 0; i < 3; i++){
        matrix[i][0] = vector[i];
    }

    Quote Originally Posted by plasticstone View Post
    do I understand correctly that matrix[0] is an adress to the first column?
    Again, "It depends on how do you interpret rows and columns", but since C is row-major, no matrix[0] is the address of the first row.


    Quote Originally Posted by plasticstone View Post
    i would like to change the adress of first column to the adress of the vector.
    You can't do that, not in this way. Using an array of pointers is a workaround, but not a pretty one.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculate column-wise sum of a matrix in C
    By Raymond2010 in forum C Programming
    Replies: 4
    Last Post: 06-21-2011, 11:21 PM
  2. sort elements of 2D matrix by 1st column reference
    By cfdprogrammer in forum C Programming
    Replies: 12
    Last Post: 05-04-2009, 03:26 PM
  3. Pls help me ! Change Matrix
    By akinal in forum C++ Programming
    Replies: 4
    Last Post: 04-16-2009, 11:12 AM
  4. column-major matrix Q....
    By pxleyes in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 07:26 PM
  5. StringGrids and how to change column width???
    By cmangel518 in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 10:59 AM

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