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.
Printable View
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.
which type of variable are the matrix elements and how are them stored in the file?
Quote:
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.
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 dataQuote:
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 ....
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!
The numbers have a defined length or they are separeted by spaces ' ' or other character?
Yes,the row number or column number areQuote:
Originally posted by Doctor Luz
The numbers have a defined length or they are separeted by spaces ' ' or other character?
fixed for now,and they are separated by space' '.
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.