Reputation: 21
Using Math.Net Numerics, how can I index parts of a matrix?
For example, I have a collection of ints and I want to get a submatrix with the rows and columns chosen accordingly.
A[2:3,2:3]
should give me that 2 x 2 submatrix of A where the row index and the column index is either 2 or 3
Upvotes: 2
Views: 3238
Reputation: 511
I suspect you were looking for something like this Extension method.
public static Matrix<double> Sub(this Matrix<double> m, int[] rows, int[] columns)
{
var output = Matrix<double>.Build.Dense(rows.Length, columns.Length);
for (int i = 0; i < rows.Length; i++)
{
for (int j = 0; j < columns.Length; j++)
{
output[i,j] = m[rows[i],columns[j]];
}
}
return output;
}
I've omitted the exception handling to ensure rows and columns are not null.
Upvotes: 0
Reputation: 23833
Just use some thing like
var m = Matrix<double>.Build.Dense(6,4,(i,j) => 10*i + j);
m.Column(2); // [2,12,22,32,42,52]
to access the desired column use the Vector<double> Column(int columnIndex)
extension method.
Upvotes: 2