# General Purpose Matrix Operations

 Home Matrix Home General Purpose Matrix Routines Home Copy Matrices / Vectors Get Row / Column of a Matrix Set Row / Column of a Matrix Get / Set the Diagonal of a Matrix Set Matrix Diagonal Elements to a Scalar Get / Set a Submatrix Fill Matrix with Scalar Join Matrices Interchange Rows / Columns Transpose Matrix

## Joining two matrices along the rows / columns.

Given an m×n1 matrix A = (aij) and an m×n2 matrix B = (bij), then an m×(n1+n2) matrix
C = (cij) may be formed by concatenating the rows of A with the rows of B, i.e. if 0 ≤ i < m, 0 ≤ j < n1 then cij = aij and if 0 ≤ i < m, n1 ≤ j < n1+n2 then cij = bi,j-n1.

Similarly given an m1×n matrix A = (aij) and an m2×n matrix B = (bij), then an (m1+m2n matrix C = (cij) may be formed by concatenating the columns of A with the columns of B, i.e. if 0 ≤ i < m1, 0 ≤ j < n then cij = aij and if m1 ≤ i < m1+m2, 0 ≤ j < n then cij = bi-m1,j.

The routines for concatenating two real matrices of type double by rows or columns are called Join_Matrices_by_Rows() and Join_Matrices_by_Columns() respectively and the routines for concatenating two complex matrices of type double complex by rows or columns are called Join_CMatrices_by_Rows() and Join_CMatrices_by_Columns() respectively.

There are 2 versions of the Join_Matrices_by_Rows() routine and 2 versions of the Join_Matrices_by_Columns() routine. For both the Join_Matrices_by_Rows() and the Join_Matrices_by_Columns() routine, 1 version is written in C and 1 version is written in NASM assembly language.

There is 1 version of the Join_CMatrices_by_Rows() routine and 1 version of the Join_CMatrices_by_Columns() routine, both versions written in C.

### Function List

• void Join_Matrices_by_Rows( double *C, double *A, int nrows, int ncols, double *B, int mcols )

Copy the nrows × ncols matrix A into the nrows × (ncols + mcols) matrix C and then copy the nrows × mcols matrix B starting at the location C[ncols], i.e. C = [A:B]. The matrix C should be declared as double C[nrows][ncols + mcols] in the calling routine.

• void Join_Matrices_by_Columns( double *C, double *A, int nrows, int ncols, double *B, int mrows )

Copy the nrows × ncols matrix A into the (nrows + mrows) × ncols matrix C and then copy the mrows × ncols matrix B starting at the location C[nrows], i.e.
CT = [A T:B T] T, where T denotes the transpose. The matrix C should be declared as double C[nrows + mrows][ncols] in the calling routine.

• void Join_CMatrices_by_Rows( double complex *C, double complex *A, int nrows, int ncols, double complex *B, int mcols )

Copy the nrows × ncols complex matrix A into the nrows × (ncols + mcols) complex matrix C and then copy the nrows × mcols complex matrix B starting at the location C[ncols], i.e. C = [A:B]. The matrix C should be declared as double complex C[nrows][ncols + mcols] in the calling routine.

• void Join_CMatrices_by_Columns( double complex *C, double complex *A, int nrows, int ncols, double complex *B, int mrows )

Copy the nrows × ncols complex matrix A into the (nrows + mrows) × ncols complex matrix C and then copy the mrows × ncols complex matrix B starting at the location C[nrows], i.e.
CT = [A T:B T] T, where T denotes the transpose. The complex matrix C should be declared as double complex C[nrows + mrows][ncols] in the calling routine.

#### C Source Code

• The file, join_by_rows.c, contains the version of Join_Matrices_by_Rows() written in C.

• The file, join_by_cols.c, contains the version of Join_Matrices_by_Columns() written in C.

• The file, join_by_crows.c, contains the version of Join_CMatrices_by_Rows() written in C.

• The file, join_by_ccols.c, contains the version of Join_CMatrices_by_Columns() written in C.

#### NASM Source CodeFor Linux gcc Users

• The file, join_by_rows.asm, contains the version of Join_Matrices_by_Row() written in NASM.

• The file, join_by_cols.asm, contains the version of Join_Matrices_by_Column() written in NASM.

#### C Test Code, Test Results, and Build Shell Script

• The file, testjoinbyrows.c, contains a test program of Join_Matrices_by_Rows( ) in the file join_by_rows.c. This test program requires the file join_by_rows.c listed above.

• The file, JoinByRowsTest.txt, contains the results of the test program testjoinbyrows.c.

• The file, testjoinbyrows.sh, contains the shell script used to compile, link, and execute the test program testjoinbyrows.c.

• The file, testjoinbycols.c, contains a test program of Join_Matrices_by_Columns( ) in the file join_by_cols.c. This test program requires the file join_by_cols.c listed above.

• The file, JoinByColsTest.txt, contains the results of the test program testjoinbycols.c.

• The file, testjoinbycols.sh, contains the shell script used to compile, link, and execute the test program testjoinbycols.c.

• The file, testjoinbycrows.c, contains a test program of Join_CMatrices_by_Rows( ) in the file join_by_crows.c. This test program requires the file join_by_crows.c listed above.

• The file, JoinByCRowsTest.txt, contains the results of the test program testjoinbycrows.c.

• The file, testjoinbycrows.sh, contains the shell script used to compile, link, and execute the test program testjoinbycrows.c.

• The file, testjoinbyccols.c, contains a test program of Join_CMatrices_by_Columns( ) in the file join_by_ccols.c. This test program requires the file join_by_ccols.c listed above.

• The file, JoinByCColsTest.txt, contains the results of the test program testjoinbyccols.c.

• The file, testjoinbyccols.sh, contains the shell script used to compile, link, and execute the test program testjoinbyccols.c.