none
Redim Matrix RRS feed

  • Question

  • Hi,

    I have been trying to Redim a matrix by multiplying the rows by 2 and preserve the data.

    The idea is to arrange the values in the following format.

    From Matrix 1

      Member ID Start Node End Node
    Matrix 1= A 1 2
      B 3 4
      C 5 6

    To Matrix 2

      Member ID Nodes Load Case
    Matrix 2= A 1 1
      A 2 1
      A 1 2
      A 2 2
      B 3 1
      B 4 1
      B 3 2
      B 4 2
      C 5 1
      C 6 1
      C 5 2
      C 6 2

    The code is something like this.

    Dim Matrix1(Rows1,Cols1) As Integer
    Dim Matrix2(Rows2,Cols1) As Integer
    
    'Matrix 1
    For i = 1 to Rows1
      For j = 1 to Cols1
        Matrix1(i,j) = Worsheets.Cells(i,j).Value
      Next j
    Next i
    
    Load Cases = 2
    NumberOfNodes = 2
    Rows2 = Load Cases*NumberOfNodes*Rows1
    
    'Matrix 2
    For i = 1 to Rows2 Step 2
      For j = 1 to Cols1
        Matrix2(i,j) = Matrix1(i,2).Value
        Matrix2(i+1,j) = Matrix1(i,3).Value
      Next j
    Next i
    

    Please let me know if you need more information.

    My email is taitobass@hotmail.com

    Thanks

    Saturday, November 4, 2017 12:14 AM

All replies

  • Option Explicit
    
    Sub Test()
      Dim DataIn, DataOut
      Dim a As Long, b As Long, c As Long, n As Long
    
      'Read the values into a 2D array
      DataIn = Range("A1:C3").Value
    
      'Calculate size for the output
      For a = 1 To UBound(DataIn)
        n = n + (DataIn(a, 3) - DataIn(a, 2) + 1) ^ 2
      Next
      'Create space for the output
      ReDim DataOut(1 To n, 1 To 3)
    
      'Compile the output
      n = 0
      For a = 1 To UBound(DataIn)
        For b = DataIn(a, 2) To DataIn(a, 3)
          For c = DataIn(a, 2) To DataIn(a, 3)
            n = n + 1
            DataOut(n, 1) = DataIn(a, 1)
            DataOut(n, 2) = b
            DataOut(n, 3) = c - DataIn(a, 2) + 1
          Next
        Next
      Next
    
      'Flush into the sheet
      Range("F1").Resize(UBound(DataOut), UBound(DataOut, 2)).Value = DataOut
    End Sub

    Saturday, November 4, 2017 6:14 PM