Thread: How to convert Row, Col to Index and Index to Row, Col

1. How to convert Row, Col to Index and Index to Row, Col

Code:
``` +-----+-----+
| 0,0 | 1,0 |
|  0  |  2  |
+-----+-----+
| 0,1 | 1,1 |
|  1  |  3  |
+-----+-----+```
I need to make two functions in my project. One function will take the row and column numbers and the size of the array and return the index number. The other function will take index number and the size of the array and return the row and column number.

My example above shows a 4-cell array which has the row,col and index of each cell. How do I write these two functions.

Code:
```   '
'
Private Function RowColToIndex(Row As Integer, Col As Integer, NumRows As Integer, NumCols As Integer) As Integer
'
' ???
'
End Function

Private Function IndexToRowCol(ByRef Row As Integer, ByRef Col As Integer, Index As Integer, NumRows As Integer, NumCols As Integer)
'
' The row and col will be returned in the arguments Row and Col
'
' ???
'
End Function```

2. Re: How to convert Row, Col to Index and Index to Row, Col

It looks like from your example, rows change in sequence with Index, so you use the number of Rows to calculate Index, row and col.

Index = col * NumRows + Row

col = Index \ NumRows
row = Index Mod NumRows

Since Index to RowCol doesn't return a value (it modifies the arguments passed), it should probably be a Sub, rather than a function.

3. Re: How to convert Row, Col to Index and Index to Row, Col

It appears that your Index = col * NumRows + Row isn't quit correct

I think it should be Index = Row * NumCols + Col. At least when I tried this it gave me the index I was looking for.

I haven't tried your other answer so I'm not sure if it is correct or not

4. Re: How to convert Row, Col to Index and Index to Row, Col

I based my math on the illustration you gave.
Generally Row, Col is given in the order (Row, Col) which would be analogous to (Y, X), i.e. Rows go down the Y axis, and columns go across the X axis.

Your diagram is giving the Row, Col in the order, (Col, Row) which is (X, Y) so I used that in the math.

Here is your diagram, and the corresponding results of the math that calculates Index from the (Col, Row) order as shown in your diagram.
Code:
```  Col 0,Col 1
+-----+-----+
| 0,0 | 1,0 |  Row 0
|  0  |  2  |
+-----+-----+
| 0,1 | 1,1 |  Row 1
|  1  |  3  |
+-----+-----+

(Col, Row)  Col * rows + row = Index    Summary
( 0,   0) :  0  * 2    + 0   = 0        (0,0) 0
( 0,   1) :  0  * 2    + 1   = 1        (0,1) 1
( 1,   0) :  1  * 2    + 0   = 2        (1,0) 2
( 1,   1) :  1  * 2    + 1   = 3        (1,1) 3```
Since these values are easily swapped around, i.e. which value is changing in sequence with the index either row or column, then you have to ensure which is appropriate for your situation.

Since you said Index = Row * NumCols + Col is correct, then you are actually using (Row, Col), and your diagram should have been :
Code:
```  Col 0,Col 1
+-----+-----+
| 0,0 | 0,1 |  Row 0
|  0  |  1  |
+-----+-----+
| 1,0 | 1,1 |  Row 1
|  2  |  3  |
+-----+-----+

Summary
(0,0) 0
(0,1) 1
(1,0) 2
(1,1) 3```
You'll note that the (a,b) input gives the desired index output in both cases, its just in the original case, you diagrammed a=col, b = row, when in fact (apparently), a=row, b= col in your situation, which is the norm, and the index increments horizontally across the columns, then down the rows)

Likewise, the reverse conversion would also be reversed to match (Row, Col) order, i.e. divide and Mod by number of columns, not number of rows:
row = Index \ NumCols
col = Index Mod NumCols

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured

Click Here to Expand Forum to Full Width