Динамическая верхняя граница для двумерного массива - PullRequest
1 голос
/ 07 мая 2019

Я определяю значения для матрицы с 92 строками и различным диапазоном столбцов (от 1 до 64 столбцов). В зависимости от номера строки, мой код должен определять верхнюю границу количества столбцов для этой строки. Я использую вложенные циклы, но мой код дает мне матрицу 92x64 (поэтому число столбцов является постоянным).

Dim m As Integer
Dim n As Integer
Dim o As Integer
Dim p As Integer
Dim q As Integer
Dim N_bay As Single
Dim N_b As Single
Dim D_r As Single
Dim s As Single
Dim Con_l As Single
Dim tau_s As Single
Dim N_r As Single

D_r = 394.9
s = 4.24
Con_l = 6.1
N_r = 92
N_b = 64

For n = LBound(N_rj, 1) To UBound(N_rj, 1)
    For m = LBound(N_rj, 2) To UBound(N_rj, 2)
        N_rj(n, 1) = n
        N_rj(n, 2) = (D_r - ((n - 1) * s))
        N_rj(n, 3) = WorksheetFunction.RoundDown(((D_r - ((n - 1) * s)) / Con_l), 0)
        N_rj(n, 4) = 1 / (N_rj(n, 3))
        size = N_rj(n, 3)
        ReDim N_bz(1 To 92, 1 To size)
            For o = 1 To UBound(N_bz, 1)
                For p = 1 To UBound(N_bz, 2)
                    N_bz(o, p) = p * Con_l
                    Cells(o + 1, p + 6).Value = N_bz(o, p)
                Next p
            Next o

        Cells(n + 1, m).Value = N_rj(n, m)
    Next m
Next n

Я ожидаю получить матрицу с 92 строками, из которых каждая строка имеет различное количество столбцов. Следовательно, строка № 1 имеет 64 столбца, строка № 2 имеет 63 столбца и т. Д.

1 Ответ

1 голос
/ 07 мая 2019

Я подозреваю, что вы пытаетесь сделать что-то вроде следующего

Sub MatrixArray()
    Dim m As Long, n As Long, o As Long, p As Long, q As Long
    Dim size As Long

    Dim N_bay As Single, N_b As Single, D_r As Single, s As Single, Con_l As Single
    Dim tau_s As Single, N_r As Single

    Dim N_rj(1 To 92, 1 To 4) As Single, N_bz() As Single

    Application.ScreenUpdating = False

    D_r = 394.9
    s = 4.24
    Con_l = 6.1
    N_r = 92
    N_b = 64

    For n = LBound(N_rj, 1) To UBound(N_rj, 1)
        For m = LBound(N_rj, 2) To UBound(N_rj, 2)
            N_rj(n, 1) = n
            N_rj(n, 2) = (D_r - ((n - 1) * s))
            N_rj(n, 3) = WorksheetFunction.RoundDown(((D_r - ((n - 1) * s)) / Con_l), 0)
            N_rj(n, 4) = 1 / (N_rj(n, 3))
            size = N_rj(n, 3)
            Debug.Print size

            ReDim N_bz(1 To 92, 1 To size)

            For o = 1 To UBound(N_bz, 1)
                For p = 1 To UBound(N_bz, 2)
                    N_bz(o, p) = p * Con_l
                Next p
            Next o

            Cells(n, 7).Resize(1, UBound(N_bz, 2)).Value2 = Application.Index(N_bz, n, 0)

        Next m
    Next n

    Cells(1, 1).Resize(UBound(N_rj, 1), UBound(N_rj, 2)) = N_rj

    Application.ScreenUpdating = True
End Sub

Что-то вроде следующего может быть более эффективным, хотя

Sub MatrixArray()
    Dim m As Long, n As Long, o As Long, p As Long, q As Long
    Dim size As Long

    Dim N_bay As Single, N_b As Single, D_r As Single, s As Single, Con_l As Single
    Dim tau_s As Single, N_r As Single

    Dim N_rj(1 To 92, 1 To 4) As Single, N_bz() As Single

    Application.ScreenUpdating = False

    D_r = 394.9
    s = 4.24
    Con_l = 6.1
    N_r = 92
    N_b = 64

    Dim MaxSize As Long
    MaxSize = WorksheetFunction.RoundDown(((D_r - ((LBound(N_rj, 1) - 1) * s)) / Con_l), 0)
    Debug.Print MaxSize
    ReDim N_bz(1 To MaxSize)

    For p = 1 To UBound(N_bz)
        N_bz(p) = p * Con_l
    Next p

    For n = LBound(N_rj, 1) To UBound(N_rj, 1)
        N_rj(n, 1) = n
        N_rj(n, 2) = (D_r - ((n - 1) * s))
        N_rj(n, 3) = WorksheetFunction.RoundDown(((D_r - ((n - 1) * s)) / Con_l), 0)
        N_rj(n, 4) = 1 / (N_rj(n, 3))
        size = N_rj(n, 3)

        Cells(n, 7).Resize(1, size).Value2 = N_bz
    Next n

    Cells(1, 1).Resize(UBound(N_rj, 1), UBound(N_rj, 2)) = N_rj

    Application.ScreenUpdating = True
End Sub
...