|Is a matrix rectangular|
|Algorithms - Matrices|
|Written by Jan Schulz|
|Sunday, 13 July 2008 20:11|
Matrix rectangularity and dimensions
Clean coding reduces the risk that a matrix is not rectangular. Anyhow, being rectangular is a crucial preliminary for a couple of matrix operations and justifies an own procedure for verification. The here introduced function returns a Boolean state, whether the matrix is rectangular or not. Additionally the maximum number of rows and columns is returned in the passed variables.
The function first determines the highest index of the row array of the matrix and stores the result in MaxRows. The dummy variable MaxCol is initialised with -1. As the lowest possible index of a matrix element is zero this value indicates that so far no column elements were found. When at least one row exists the function proceeds and initialises the variable aColInit with the highest index number of column elements in the first matrix row. Thereafter the highest index number of column elements is determined for every row. When this value differs at least once from the value in aColInit the matrix can not be rectangular. When it is further higher than the value in MaxCols the old value is overwritten. After processing all rows the value 1 is added to MaxRows and MaxCols to get the absolute number of elements and not the respective highest 0-based index.
Function mtx_IsRectangular (InputMatrix : t2dVariantArrayDouble; Var MaxRows, MaxCols: Integer): Boolean;
For a data matrix aInputMatrix of the type t2dVariantArrayDouble, populated with:
the call of:
aBooleanVar := mtx_IsRectangular (aInputMatrix, aMaxRow, aMaxCol);
returns TRUE, a value of 6 in aMaxRows and a value of 3 in aMaxCols.
|Last Updated on Friday, 18 March 2011 18:06|