CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2003
    Posts
    15

    Matrix Assigment

    I have a matrix to assign its'value
    the values are stored in a data file,
    for example :
    1 2 3 4 5
    5 6 7 8 9
    .....
    How can I read the values from file
    into the Matrix?Thanks.

  2. #2
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688
    which type of variable are the matrix elements and how are them stored in the file?

  3. #3
    Join Date
    Jan 2003
    Posts
    15
    Originally posted by Doctor Luz
    which type of variable are the matrix elements and how are them stored in the file?

    I want to know if there is a good way to deternmine
    from the data file, for example 2 rows 5 cols and
    alloc enough memory for the array and then read
    data sequentially from the data file and assign each value to the corresponding position?Thanks.

  4. #4
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688
    to alloc data you should know which type of data to alloc. int, float, double....

    And How was this data stored in file, for example if the data was stored as formatted text, binary ....

  5. #5
    Join Date
    Jan 2003
    Posts
    15
    Originally posted by Doctor Luz
    to alloc data you should know which type of data to alloc. int, float, double....

    And How was this data stored in file, for example if the data was stored as formatted text, binary ....
    I know the data type for example double,the data
    is stored in a simple text file,the format of is like:
    1.0 2.0 3.0 4.0 5.0
    3.0 4.0 5.0 6.0 4.0
    the first row of the data from the text file goto the first column of the matrix;the second row goto the second
    column of the matrix.Is there any convienient way
    to do that?Thanks a lot!

  6. #6
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688
    The numbers have a defined length or they are separeted by spaces ' ' or other character?

  7. #7
    Join Date
    Jan 2003
    Posts
    15
    Originally posted by Doctor Luz
    The numbers have a defined length or they are separeted by spaces ' ' or other character?
    Yes,the row number or column number are
    fixed for now,and they are separated by space' '.

  8. #8
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688
    First alloc in memory the matrix elements. For example if the matrix has ( row X col) elements you can do:

    Code:
    matrix =  (double **) calloc (rows, sizeof (double **));
         for  (int i = 0; i < rows; i++)
              matrix [i] = (double *) calloc (cols, sizeof (double));

    After this You can read each text row, extract it's elements taking into account the ' ' is the separator and them to the matrix[row][col] double element.

    This FAQ explains how to convert a string to a numeric type.

    dont forget to free(matrix) after use it.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured