**6.3****
****Value Operations on Matrices** (.NET, C#, CSharp, VB, Visual Basic, F#)

The matrix classes have the following read-only properties:

● Cols gets the number of columns in a matrix.

● ColStride gets the step increment between successive elements in a column.

● Rows gets the number of rows in a matrix.

● RowStride gets the step increment between successive elements in a column.

● DataBlock gets a reference to the data block that a matrix is viewing.

For example, if A is a
**FloatComplexMatrix** instance:

Code Example – C# matrix

int cols = A.Cols; int rows = A.Rows; FloatComplexDataBlock block = A.DataBlock;

Code Example – VB matrix

Dim Cols As Integer = A.Cols Dim Rows As Integer = A.Rows Dim Block As FloatComplexDataBlock = A.DataBlock

**NOTE—****As
described in ****Section ****4.1****,
use caution when accessing a data block referenced by a matrix.
Other objects may be viewing the same data. **

**Accessing and Modifying Matrix Values**

The matrix classes provide standard indexers
for getting and setting element value at a specified row and column position
in a matrix. Thus, A[i,j] always returns
the element in the *i*th row and* j*th column of matrix A's
view of the data.

**NOTE—****Indexing
starts at 0.**

Thus, this code sets the value in the lower right corner of the matrix to zero:

Code Example – C# matrix

var A = new DoubleMatrix( "2x2 [1 2 3 4]" ); A[1,1] = 0;

Code Example – VB matrix

Dim A As New DoubleMatrix("2x2 [1 2 3 4]") A(1, 1) = 0

The matrix indexer is also overloaded to accept **Range** and **Slice**
indexing objects. For instance:

Code Example – C# matrix

var A = new DoubleMatrix( 5, 5, 2); var B = new DoubleMatrix( 2, 2, 1); var s = new Slice( 0, 2 ); A[s,s] = B

Code Example – VB matrix

Dim A As New DoubleMatrix(5, 5, 2) Dim B As New DoubleMatrix(2, 2, 1) Dim S As New Slice(0, 2) A(S, S) = B

You can also use the Set() member function to set the data elements of a matrix to a specified value. For instance, this code sets values in the last two columns of matrix A to zero:

Code Example – C# matrix

int rows = 5, cols = 5; var A = new DoubleMatrix( rows, cols, 0, 1 ); var col = new Slice( 3, 2 ); Slice row = Slice.All; A.Set( col, row, 0 );

Code Example – VB matrix

Dim Rows As Integer = 5 Dim Cols As Integer = 5 Dim A As New DoubleMatrix(Rows, Cols, 0, 1) Dim Col As New Slice(3, 2) Dim Row As Slice = Slice.All A.Set(Col, Row, 0)

You can replace either slice with an integer value indicating a particular row or column. Thus, this code changes the values in the first column of A to -1:

Code Example – C# matrix

A.Set( 1, Slice.All, -1);

Code Example – VB matrix

A.Set(1, Slice.All, -1)

**NOTE—****Any
method that returns a vector view of the data referenced by a matrix
can be used to modify the values of matrix, since the returned vector
and the matrix share the data. See ****Section ****6.6****.**

**Clearing and Resizing a Matrix**

The matrix classes provide two methods for changing the size of a matrix after it has been created:

● Clear() resets the value of all data elements to zero.

● Resize() changes the size of a matrix to the specified number of rows and columns, adding zeros or truncating as necessary.

● ResizeAndClear() performs the same function as Resize(), but also resets the value of all remaining data elements to zero.