Kardi Teknomo
Kardi Teknomo Kardi Teknomo Kardi Teknomo
     
 
Research
Publications
Tutorials
Resume
Personal
Resources
Contact

 


How to Iterate over a Symmetric Table?

By Kardi Teknomo

 

This is a very simple tutorial on programming for fresh programmers. The problem goes like this:

You have a symmetric table or matrix size by and you want to do something (i.e. evaluate or simply print) based on the content of the matrix. Because the matrix is symmetric, you do not need to iterate over the whole matrix. You can iterate over upper triangular part only or lower triangular part only and get the same results. How to do that in the program?

 

Solution:

To be precise, you have square symmetric matrix

and you want to iterate over …

•  Upper triangular matrix without diagonal, or

•  Upper triangular matrix with diagonal, or

•  Lower triangular matrix without diagonal, or

•  Lower triangular matrix with diagonal

 

 

Upper triangular matrix without diagonal

 

Pseudo Code: 
For row = 1 to (q-1) 
     For col = (row + 1) to q 
          Print A (row, col) ‘// do something 
     Next col 
Next row 

 

Number of members printed:

Upper triangular matrix with diagonal

 

Pseudo Code: 
For row = 1 to q 
     For col = row to q 
          Print A (row, col) ‘// do something 
     Next col 
Next row 

 

Number of members printed:

 

 

Lower triangular matrix without diagonal

Pseudo Code: 
For col = 1 to (q-1) 
     For row = (col + 1) to q 
          Print A (row, col) ‘// do something 
     Next col 
Next row 

 

Number of members printed:

Lower triangular matrix with diagonal

Pseudo Code: 
For col = 1 to q 
     For row = col to q 
          Print A (row, col) ‘// do something 
     Next col 
Next row 

 

Number of members printed:

 

 

See Also: VB Tutorial

 
 
© 2005 Kardi Teknomo. All Rights Reserved.
Designed by CNV Media