# Creating a diagonal matrix in VB 6

vb6 , vb 6 , diagonal matrices
• 12 years ago
Hi. I am having trouble defining a diagonal matrix. I am using code that is something like public diagmatrix(numofm, numofm) as long (numofm has been pre-set as some number) but i'm getting an error message, "constant expression required". Thing is, to begin with, the number of columns and rows (square matrix) should be equal to "numofm"; this is because "numofm" will change on different occassions as selected by users and the code " public diagmatrix(numofm, numofm) as long" was meant to allow the matrix dimensions to change accordingly. Secondly, the diagonal elements are dependent on other variables, say, 3 variables, temperature, pressure and thrust. This would thus require a 3by3 diagonal matrix. How would I define these diagonal elements ? Say, Row1, column 1 should be reciprocal of temp. Row2, column 2 should be reciprocal of pressure Row3, column 3 should be reciprocal of thrust Every other element would ofcoure be zero, being a diagonal matrix. Temp, pressure and thrust are variables and users can add to these, thus requiring a larger matrix---or reduce these variables, thus requiring a smaller matrix. Thanks.
• 12 years ago
You should declare the matrix like this: public diagmatrix() as long then in code you must dimension the matrix with: redim diagmatrix(1 to numofm, 1 to numofm) this can be done any number of times as required. Every time the matrix will be cleared. I didn't understand the second part of your problem.
• 12 years ago
Thanks for the first part. The second part---- well, imagine a 3 by 3 matrix. Row 1, Column 1 = some value, Row 2 Column 2 = some value and Row 3, Column 3 = some value. Everything else in the matrix = 0. How do you do that in VB 6 ?
• 12 years ago
Thanks for the first part. The second part---- well, imagine a 3 by 3 matrix. Row 1, Column 1 = some value, Row 2 Column 2 = some value and Row 3, Column 3 = some value. Everything else in the matrix = 0. How do you do that in VB 6 ?
• 12 years ago
diagmatrix(1,1) = some value diagmatrix(2,2) = some value diagmatrix(3,3) = some value Everything else is zero, because the REDIM statement clears the matrix.
• 12 years ago
Thanks very much, your assistance has been quite useful. Just one more thing now; I need to write this matrix to a text file somewhere, and then read it when I need it. The writing part is okay, no problem. The reading part however is a problem--how do you read a matrix from a file and use it in your VB program ? Character by character or as a whole ? And what would be the syntax ? Thanks.
• 12 years ago
diagmatrix(1,1) = some value diagmatrix(2,2) = some value diagmatrix(3,3) = some value good code [ahşap dekorasyon ](http://www.ahsapdekorasyonbul.com)
• 12 years ago
Whenever I need to save values to a text file and then read them back, I use something like this: To write the data: FH = FreeFile OPEN FileName FOR OUTPUT AS #FH WRITE #FH, diagmatrix(1,1) WRITE #FH, diagmatrix(2,2) WRITE #FH, diagmatrix(3,3) CLOSE #FH To read it back: FH = FreeFile OPEN FileName FOR INPUT AS #FH INPUT #FH, diagmatrix(1,1) INPUT #FH, diagmatrix(2,2) INPUT #FH, diagmatrix(3,3) CLOSE #FH I have a question, why do you need a matrix ?, why not use just a single dimension array ?
• 12 years ago
Thanks very much for the code. I am using a matrix just coz the discipline i'm applying it to uses matrices and it's even easier for me to think of the variables as matrices and not single dimension arrays. Once again, thankyou very much for the code. God bless you.

## Post a reply

### Enter your message below

Sign in or Join us (it's free).

## Quick links

### Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

### Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.”