How to calculate and print/write out Trip Ends from a Matrix


The Rowsum() function can be used in a Matrix program to calculate matrix Tripe Ends.

Trip Ends are row and column totals (Productions and Attractions).

To calculate column totals using the Rowsum function, you should transpose your input matrix first. 

The following script gives you an example of Trip Ends calculation and printing to a text file. 

RUN PGM=MATRIX PRNFILE="EXMAT00D.PRN"

FILEI MATI[1]="File_Name.MAT"	; Binary Matrix File
FILEO PRINTO[1]="File_Name.TXT	; TXT Output File

MW[1]=MI.1.1					; Work Matrix 1 from Tab 1 of Input Matrix 1
MW[2]=MI.1.1.T					; Work Matrix 2 – MI.1.1 Transpose

Zone=I							; Zone Number
Prod=ROWSUM(1)					; Production = Row total from MW[1]
Attr=ROWSUM(2)					; Attraction = Row total from MW[2]

Print FORM=10.2 PRINTO=1 LIST=Zone,Prod,Attr	
; Print in the output file 1 the trip ends (LIST)
; with the format stated with FORM (10 digits with 2 decimals)

ENDRUN

The following script gives you an example of Trip Ends calculation and writing to a DBF file:

RUN PGM=MATRIX PRNFILE="EXMAT00D.PRN"

FILEI MATI[1]="File_Name.MAT"			; Binary Matrix File
FILEO RECO[1]="File_Name.DBF",		; TXT Output File
      FIELDS=Zone,Prod,Attr

MW[1]=MI.1.1						; Work Matrix 1 from Tab 1 of Input Matrix 1
MW[2]=MI.1.1.T						; Work Matrix 2 – MI.1.1 Transpose

RO.Zone=I							; Zone Number
RO.Prod=ROWSUM(1)					; Production = Row total from MW[1]
RO.Attr=ROWSUM(2)					; Attraction = Row total from MW[2]

WRITE RECO=1

ENDRUN

<<<<

Note: to calculate the matrix Grand Total, you simply accumulate the row total in a variable, like in the example below:

_grand_tot_mat_1 = _grand_tot_mat_1 + rowsum(1)