ReadOnlyDoubleMatrixItem Property (Int32) |
Namespace: Novacta.Analytics
Exception | Condition |
---|---|
ArgumentOutOfRangeException | linearIndex is less than zero. -or- linearIndex is equal to or greater than the Count of this instance. |
Let be a matrix, and consider its generic entry
where and
are the
number of rows and columns of , respectively.
A linear index completely
identifies an entry,
assuming that entries are linearly ordered following the
ColumnMajor
data order. This means that entry has linear
index equal to , and matrix entries can be enumerated as
follows:
where is the Count of the matrix.
In the following example, a matrix element is accessed using its linear index.
using System; namespace Novacta.Analytics.CodeExamples { public class LinearIndexerExample0 { public void Main() { // Create a matrix. var data = new double[6] { 10, 20, 30, 40, 50, 60 }; var matrix = DoubleMatrix.Dense(2, 3, data, StorageOrder.RowMajor); Console.WriteLine("Initial data matrix:"); Console.WriteLine(matrix); // Specify a linear index. int linearIndex = 3; Console.WriteLine(); Console.WriteLine("Linear index: {0}", linearIndex); // Set the corresponding entry. matrix[linearIndex] = -50.0; Console.WriteLine(); Console.WriteLine("Updated data matrix:"); Console.WriteLine(matrix); // Entries can also be accessed using a read-only wrapper of the matrix. ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly(); Console.WriteLine(); Console.WriteLine("Updated matrix entry:"); Console.WriteLine(readOnlyMatrix[linearIndex]); } } } // Executing method Main() produces the following output: // // Initial data matrix: // 10 20 30 // 40 50 60 // // // // Linear index: 3 // // Updated data matrix: // 10 20 30 // 40 -50 60 // // // // Updated matrix entry: // -50