How to recursively adding up matrices


If you have a certain number of matrices that you want to sum up, you can do this easily in a Matrix program. The catalog below provides an example with an input matrix (test matrix generated in the script) with 20 matrices:

communities.bentley.com/.../ex_5F00_matrix_5F00_sum_5F00_loop.zip

The first Matrix program generates the test matrix. The second Matrix program contains the script below:

RUN PGM=MATRIX PRNFILE="{SCENARIO_DIR}\MSAAT0AB.PRN" MSG='recursively sum'

FILEO MATO[1] = "{SCENARIO_DIR}\MSAAT0AB.MAT",
      MO = 100, NAME = sum
FILEI MATI[1] = "{SCENARIO_DIR}\MSAAT0AA.MAT"


fillmw mw[1] = mi.1.1(20)  ; 20 matrices in total

loop _matrix = 1, 20
  MW[100] = MW[100] + MW[_matrix]
endloop

ENDRUN

Where a loop is undertaken across the multiple matrices contained in the input file to generate the final sum matrix.