Home Algorithms Matrices Matrix creation
30 | 03 | 2017
Matrix creation PDF Print E-mail
Algorithms - Matrices
Written by Jan Schulz   
Sunday, 13 July 2008 20:10

Creating a matrix of defined dimensions

After declaration of a variable of the type t2dVariantArrayDouble the dynamic arrays have zero length and no information can be stored in the record structure. Thus, accessing arrays results in a runtime error. Here a function is introduced that initialises a rectangular matrix of given dimensions for a declared variable of the type t2dVariantArrayDouble. Elements of the matrix are initialised with a predefined value.



First the name of the matrix is set. Thereafter the desired dimensions of the row and column tile arrays of are set according to passed parameters. To create the matrix the dimension of the row array is initialised first. Perpendicular to the matrix rows the column arrays are set for the elements of the matrix. Each element is initialised with the demanded value.



Function mtx_Create (Var OutputMatrix : T2dVariantArrayDouble; SizeY, SizeX : Integer; InitialValue : Double; MatrixName :String) : Boolean;
// The function mtxCreateNew creates a new matrix of the type t2dVariantArray. The
// respective size for columns and rows are given in SizeX and SizeY.
// The matrix is initialised with the value found in InitialValue.
// The name of the matrix can be given in MatrixName.
// (c) Jan Schulz, November 2004, www.code10.info
Var RunnerX : Integer;
RunnerY : Integer;
mtx_Create := False;

// set the required length of the arrays
SetLength (OutputMatrix.ColTitle, SizeX);
SetLength (OutputMatrix.RowTitle, SizeY);

// set name of matrix
OutputMatrix.MatrixName := MatrixName;

// initialise titles with empty fields
For RunnerX := High (OutputMatrix.ColTitle) downto 0 do
OutputMatrix.ColTitle [RunnerX] := '';

For RunnerY := High (OutputMatrix.RowTitle) downto 0 do
OutputMatrix.RowTitle [RunnerY] := '';

//create and initialise elements of data matrix
SetLength (OutputMatrix.Cells, SizeY);

For RunnerY := High (OutputMatrix.Cells) downto 0 do
// set the required elements for every data row
SetLength (OutputMatrix.Cells [RunnerY], SizeX);

For RunnerX := High (OutputMatrix.Cells [RunnerY]) to 0 do
// initialise elements with given value
OutputMatrix.Cells [RunnerY, RunnerX] := InitialValue;

mtx_Create := True;



The below example creates and initialises a defined matrix for the variable aMatrix of the type t2dVariantArrayDouble with six rows and three columns. All elements aMatrix.Cells of the matrix are initialised with the value zero. The dimension of the array aMatrix.RowTitle is set to six and that of aMatrix.ColTitle to three. Fields of the respective arrays contain empty strings. The name aMatrix.MatrixName is set to ‘Example matrix’:

Procedure CreateNewMatrix;
Var aMatrix : t2dVariantArrayDouble;
  // create an initialised matrix
  mtx_CreateNew (aMatrix, 6, 3, 0, 'Example matrix');
  // use the matrix


Last Updated on Friday, 18 March 2011 18:03
Sponsored Links