Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a 1-dimensional Array from a 2-Dimensional Array 1

Status
Not open for further replies.

jmarkus

Technical User
Oct 15, 2002
124
CA
If I have a 2 dimensional array:
myArray(4,2)
Can I create a 1 dimensional array
myNewArray(2) where
myNewArray(1) = myArray(4,1)
and
myNewArray(2) = myArray(4,2)

without looping through the exact process.
i.e. Is there a VB function to turn myArray(x,y) into x numbers of myNewArray(y)?

Hope that's clear.

Thanks,
Jeff
 
Clear as mud and I don't think so maybe someone else MajP, Duane or Ace has some insight :)

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
In vba you can create a jagged array (array of arrays) if the arrays are dimensioned as variants. They are a little tricky to work with. They are not the same as a multidimensional array, but they may seem like it. So to return the second item in the first dimension and the third item of the second dimension you would do
myArr(2,3)
but with a jagged array it would be
myJaggedArr(2)(3)
which returns the third item contained in the array contained in the second item of the array.

So myJaggedArr(2) would return an array.


If you can explain the big picture there may be a better approach. You could also do this with a collection or dictionary.
In vb.net Jagged arrays are very quick compared to multidimensional arrays.
 
The big picture is that I am just trying to learn if there is a more efficient way to extract information from the array. For the current code I am working with I have 1 4x2 array and 2 4x3 arrays which I want use the "rows" from each for input into a function. For this small number and dimension I have used a simple for-next loop.

But if I have a larger number of arrays, and/or of a larger dimension - it seems to me that this wouldn't be very efficient. I envisioned that "there must be an operator to refer to the row of an array" so I don't have to manipulate the data into a new variable.

Thanks,
Jeff
 
I think you are worried about nothing. To demonstrate you can run this and see that it runs almost instantaneous. My array is slightly larger than your 4x3. It is a 1Million x 3.

Build the 1Mx3 and then return the 2 dimension as a 1D array.
Code:
Public Sub LoadArray()
  Dim A(1000000, 2) As Long
  Dim OneD_A() As Long
  Dim I As Long
  For I = 0 To 1000000
    A(I, 0) = I
    A(I, 1) = I + 1000000
    A(I, 2) = I + 2000000
  Next I
 
  OneD_A = GetColumn(A, 2)
  MsgBox OneD_A(400)
End Sub
[code]

build a function to return a column (or row depends on your interpretation)
[code]
Public Function GetColumn(A() As Long, intCol As Integer) As Long()
  Dim I As Long
  Dim tempArr() As Long
  ReDim tempArr(UBound(A))
  For I = LBound(A) To UBound(A)
    tempArr(I) = A(I, intCol)
  Next
  GetColumn = tempArr
End Function
[code]


If your number of dimensions was beyond 2 or if your second dimension is large then you might consider another data structure. You may want to look at jagged arrays.  Arrays are not internally stored with "rows" and "columns". So even if it they exposed a means to get one of the dimensions, that function would still have to iterate the array.

Languages like VB.net provides lots of different data structures such as collections, heaps, ordered arrays, lists, dictionaries...
Efficiency really depends on how you use it and which one to choose.  There is a trade off between Sorting, adding, removing, and finding.  There is not one structure that is best. For example determining if an item exists in a dictionary is very efficient compared to an array. But adding to a sorted list or a dictionary requires overhead as compared to an array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top