I’ve got another question with List of lists. Once again, I have the generic matrix class as below.
public class Matrix<T>
{
List<List<T>> matrix;
public Matrix()
{
matrix = new List<List<T>>();
}
public void Add(IEnumerable<T> row)
{
List<T> newRow = new List<T>(row);
matrix.Add(newRow);
}
}
// Test code
Matrix<double> matrix = new Matrix<double>();
matrix.Add(new List<double>() { 0, 0 });
matrix.Add(new List<double>() { 16.0, 4.0 });
I’m reading lines of strings from a text file which contains values with the following format,
4 2
0.5 0.4 0.6 0.1 10.1 11.1 0.5 12.0
The first line specifies the 4×2 matrix size.
The second line needs to be so that first 4 values are in the first column of the matrix and the last four values should be in the second column.
This is dynamic hence the size is not fixed.
Reading the lines and delimiting these lines is sorted. My question is around how to use the Matrix class to store these values. In other words how can I do the elements in a row ?
It should be done so that the matrix will look like,
0.5 10.1
0.4 11.1
0.6 0.5
0.1 12.0
Thanks in advance.
Should your matrix dimensions really be mutable? A better alternative might be to pass its dimensions inside constructor and allocate arrays inside the matrix:
Alternatively, it it does need to be mutable, you can choose to allocate it “lazy”, as needed:
Note that the second approach might do a lot of allocations if you are populating it sequentially.
I would opt for the first approach (a fixed size matrix), since in most cases its dimensions are already known. It is fastest to allocate, and it’s safe to use in a multithreaded application.
If you are reading values from a file, setting them through indices should much simpler than instantiating a new list for each row.