Click to See Complete Forum and Search --> : Matrix Assigment


yelllowstone
May 10th, 2003, 05:55 PM
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.

Doctor Luz
May 10th, 2003, 05:58 PM
which type of variable are the matrix elements and how are them stored in the file?

yelllowstone
May 10th, 2003, 06:08 PM
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.

Doctor Luz
May 10th, 2003, 06:15 PM
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 ....

yelllowstone
May 10th, 2003, 06:23 PM
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!

Doctor Luz
May 10th, 2003, 06:28 PM
The numbers have a defined length or they are separeted by spaces ' ' or other character?

yelllowstone
May 10th, 2003, 06:34 PM
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' '.

Doctor Luz
May 10th, 2003, 06:52 PM
First alloc in memory the matrix elements. For example if the matrix has ( row X col) elements you can do:


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 (http://www.codeguru.com/forum/showthread.php?s=&threadid=231054) explains how to convert a string to a numeric type.

dont forget to free(matrix) after use it.