Home Algorithms Matrices Is a matrix rectangular
14 | 10 | 2024
Is a matrix rectangular PDF Print E-mail
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.

Algorithm

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.

 

Source

Function mtx_IsRectangular (InputMatrix : t2dVariantArrayDouble; Var MaxRows, MaxCols: Integer): Boolean;
Var aColInit : Integer;
aColIndex : Integer;
RunnerY : Integer;
Begin
// we expect that it is not rectangular
mtx_IsRectangular := False;

// get the known number of rows and initialise the column counter
MaxRows := High (InputMatrix.Cells);
MaxCols := -1;

// when rows are present we investigate each of them
If MaxRows >=0 THen
Begin
// and we might assume that the matrix is rectangular
mtx_IsRectangular := True;

// get the number of elements in the first row
aColInit := High (Inputmatrix.Cells [0]);

// control all rows
For RunnerY := Low (InputMatrix.Cells) to High (InputMatrix.Cells) do
Begin
// for the respective number of elements in it
aColIndex := High (InputMatrix.Cells [RunnerY]);

// control whether the number is greater than any previous
If aColIndex > MaxCols THen MaxCols := aColIndex;

// when they differ are are undefined the matrix is not rectangular
If (aColInit <> aColIndex) Or (aColIndex < 0) THen mtx_IsRectangular := False;
end;
end;

// add one to change max indices to max number of cols/rows
MaxRows := MaxRows + 1;
MaxCols := MaxCols + 1;
end;
 

Example

For a data matrix aInputMatrix of the type t2dVariantArrayDouble, populated with:

Data

Var1

Var2

Var3

Case1

1

1

1

Case2

1

1

0

Case3

2

2

2

Case4

10

10

10

Case5

11

11

11

Case6

10

5

0

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
 
Sponsored Links