Вопрос

У меня есть массив (1,2,3), и я хотел бы сделать круговой сдвиг, чтобы получить, например, (3,1,2) или (2,3,1).В Matlab я знаю, как это сделать, используя следующий код:

 Y = circshift(A,K)

Можете ли вы помочь мне с этим заданием?Буду признателен за примеры кодов...

Это было полезно?

Решение 2

Option Explicit
Option Base 1 

Sub shiftCircArray()
Dim iInputArray(3) As Integer
iInputArray(1) = 1
iInputArray(2) = 2
iInputArray(3) = 3
Dim iArray2() As Integer
iArray2 = RotateArrayRight(iInputArray)
End Sub

Function RotateArrayRight(ArrayToRotate)
Dim objNewArray() As Integer, iOldArrayPos As Integer, iNewArrayPos As Integer, iArrayLength     
As Integer
Dim iPlacesToRotate As Integer
' Check that the array to be processed has been initialized
iPlacesToRotate = Application.CountA(ArrayToRotate)
If iPlacesToRotate <> 0 Then 
    ' Check that the number of places to rotate is greater than zero - running the
    ' function with a value
    ' of places to rotate which is less than zero would cause problems, possibly causing            
    'the function to crash
    If iPlacesToRotate > 0 Then
        ' get the length of the array to rotate, we'll be using it a few times so will
        ' load it into a local variable at the start of the function
        iArrayLength = Application.CountA(ArrayToRotate)
        ' Array will be initialised from 0 to ArrayLength -1
        ' so it will contain ArrayLength elements
        ReDim objNewArray(iArrayLength)
        ' This will remove any extra complete rotations through the array
        ' The mod operator returns the remainder of an integer divide operation
        ' Initialise the array position indexes
        iOldArrayPos = iPlacesToRotate
        iNewArrayPos = 1
        ' Copy objects from one array to the next
        ' First start at iPlacesToRotate into the old array
        ' and copy to the start of the new array.
        While iOldArrayPos < iArrayLength + 1
            objNewArray(iNewArrayPos) = ArrayToRotate(iOldArrayPos)
            iOldArrayPos = iOldArrayPos + 1
            iNewArrayPos = iNewArrayPos + 1
        Wend
            iOldArrayPos = 1
            ' Copy from the start of the old array into the end of the
            ' new array
        While iOldArrayPos < iPlacesToRotate
            objNewArray(iNewArrayPos) = ArrayToRotate(iOldArrayPos)
            iOldArrayPos = iOldArrayPos + 1
            iNewArrayPos = iNewArrayPos + 1
        Wend
    Else
    MsgBox ("Values for 'Places to Rotate' must be greater than zero.")
    End If
Else
MsgBox ("Cannot rotate an null array.")
End If
RotateArrayRight = objNewArray()
End Function

Другие советы

Код:

Private Function RotateArrayRight(ByVal ArrayToRotate As Object(), ByVal iPlacesToRotate As Integer) As Object()

        Dim objNewArray As Object()
        Dim iOldArrayPos, iNewArrayPos As Integer
        Dim iArrayLength As Integer

        ' Check that the array to be processed has been initialized
        If Not ArrayToRotate Is Nothing Then

            ' Check that the number of places to rotate is greater than zero - running the function with a value 
            ' of places to rotate whiich is less than zero would cause problems, possibly causing the function to crash
            If iPlacesToRotate > 0 Then

                ' get the length of the array to rotate, we'll be using it a few times so will
                ' load it into a local variable at the start of the function
                iArrayLength = ArrayToRotate.Length

                ' Array will be initialised from 0 to ArrayLength -1
                ' so it will contain ArrayLength elements
                ReDim objNewArray(iArrayLength - 1)

                ' This will remove any extra complete rotations through the array
                ' The mod operator returns trhe remainder of an integer divide operation
                iPlacesToRotate = iPlacesToRotate Mod iArrayLength

                ' Initialise the array position indexes
                iOldArrayPos = iPlacesToRotate
                iNewArrayPos = 0

                ' Copy objects from one array to the next
                ' First start at iPlacesToRotate into the old array
                ' and copy to the start of the new array.
                While iOldArrayPos < iArrayLength
                    objNewArray(iNewArrayPos) = ArrayToRotate(iOldArrayPos)

                    iOldArrayPos += 1
                    iNewArrayPos += 1
                End While

                iOldArrayPos = 0

                ' Copy from the start of the old array into the end of the 
                ' new array
                While iOldArrayPos < iPlacesToRotate
                    objNewArray(iNewArrayPos) = ArrayToRotate(iOldArrayPos)

                    iOldArrayPos += 1
                    iNewArrayPos += 1
                End While
            Else
                Throw New ArgumentOutOfRangeException("Values for 'Places to Rotate' must be greater than zero.")
            End If
        Else
            Throw New NullReferenceException("Cannot rotate an null array.")

        End If

        Return objNewArray

    End Function

Вы можете использовать функцию в качестве примера:

Dim iInputArray() As Object
iInputArray = {4, 9, 2, 1}
Dim iArray2(), iArray3() As Object

iArray2 = RotateArrayRight(iInputArray, 2) ' Shift right two positions
iArray3 = RotateArrayRight(iInputArray, 98977) ' Shift right 98977 positions

Редактировать:

iOldArrayPos += 1 => iOldArrayPos = iOldArrayPos+ 1
End While => Wend
Throw => MsgBox
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top