KingSun
asked on
How to create data matrix
I want to create a data matrix with following known parameters:
n: count of columns
min_int(n): min value for each column
max_int(n): max values for each clumn
for example:
n=3
min_int(1,2,3)=(1,1,1)
max-int(1,2,3)=(3,2,4)
I want to got the matrix as below:
col(1) col(2) col(3)
-------------------------
1 1 1
1 1 2
1 1 3
1 1 4
1 2 1
1 2 2
...
...
...
3 2 3
3 2 4
----------------------
Can anyone give me sample code?
n: count of columns
min_int(n): min value for each column
max_int(n): max values for each clumn
for example:
n=3
min_int(1,2,3)=(1,1,1)
max-int(1,2,3)=(3,2,4)
I want to got the matrix as below:
col(1) col(2) col(3)
-------------------------
1 1 1
1 1 2
1 1 3
1 1 4
1 2 1
1 2 2
...
...
...
3 2 3
3 2 4
----------------------
Can anyone give me sample code?
It's easy, if the number of columns is a constant value
Private Type mRow
ColValue(1 to 3) As Integer
End Type
Dim mRows() As mRow
Sub InitMatrix(min1,max1,min2, max2,min3, max3)
Dim rowsNeeded As Long
Dim actRow As Long
Dim i As Long
Dim j As Long
Dim k As Long
rowsNeeded = (max1-min1+1) * (max2-min2+1) * (max3-min3+1)
Redim mRows(1 to rowsNeeded)
actRow = 1
For i = min1 To max1
For j = min2 to max2
For k = min3 to max3
mRows(actRow).ColValue(1) = i
mRows(actRow).ColValue(2) = j
mRows(actRow).ColValue(3) = k
actRow = actRow + 1
Next k
Next j
Next i
End Sub
Let me know if you need an example with variable number of columns, meaning that matrix parameters are set at runtime and may differ each time
Private Type mRow
ColValue(1 to 3) As Integer
End Type
Dim mRows() As mRow
Sub InitMatrix(min1,max1,min2,
Dim rowsNeeded As Long
Dim actRow As Long
Dim i As Long
Dim j As Long
Dim k As Long
rowsNeeded = (max1-min1+1) * (max2-min2+1) * (max3-min3+1)
Redim mRows(1 to rowsNeeded)
actRow = 1
For i = min1 To max1
For j = min2 to max2
For k = min3 to max3
mRows(actRow).ColValue(1) = i
mRows(actRow).ColValue(2) = j
mRows(actRow).ColValue(3) = k
actRow = actRow + 1
Next k
Next j
Next i
End Sub
Let me know if you need an example with variable number of columns, meaning that matrix parameters are set at runtime and may differ each time
ASKER
Yes I want to know the answer with variable number of columns. That is the question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry to bother you again.
above code you provide is enough to create the data matrix, but one problem is when n become pretty big, like 50 or above, the row count will reach the limitation of LONG data type. for example, n=50, all columns cycle between 1 to 5, the total row number will be 5^50= 8.881784197001e+34. It's terrible.
So I think we need a new solution. My idea is that not store all rows in data array, just get 1 row, use it for some function, just throw it away. then next row. Would you pls help me again on it?
Many thank and happy new year!
above code you provide is enough to create the data matrix, but one problem is when n become pretty big, like 50 or above, the row count will reach the limitation of LONG data type. for example, n=50, all columns cycle between 1 to 5, the total row number will be 5^50= 8.881784197001e+34. It's terrible.
So I think we need a new solution. My idea is that not store all rows in data array, just get 1 row, use it for some function, just throw it away. then next row. Would you pls help me again on it?
Many thank and happy new year!
for a 4 x 5 matrix example
dim matrix(4,5)
matrix(1,1) = 1
matrix(1,2) = 4
...
...
etc.
process the matrix using for next loops.
If you're learning VB then I'd strongly recommend you to think about the problem yourself and try to get somewhere with it, then show us your code if you get stuck.
Good Luck!