我需要帮助构建焦虑矩阵,其中n等于t = 0,...,10时t时剩余股票的数量。最初,我总共有10个股票,我将丢弃一个一个随着时间流逝。
矩阵的每个元素将等于sigma(i)x sigma(j)x rho,其中sigma(i)= sigma(j)= 0.25且rho = 0.2
我想要做的最后一件事就是将该矩阵乘以该矩阵的转置
我真的很困惑在哪里以及如何开始,并且非常感谢你的帮助
Sub Matrix()
'Sigmai 'Sigmaj 'Rho
Dim Sigmai, Sigmaj, Rho As Single
Sigmai = Range("b12").Value
Sigmaj = Range("b13").Value
Rho = Range("b14").Value
Dim matrixelement(10, 10) As Single
For n = 1 To 10
For m = 1 To 10
matrixelement(n, m) = Sigmai * Sigmaj * Rho
Cells(n, m) = matrixelement(n, m)
Next m
Next n
End Sub
答案 0 :(得分:2)
您的代码存在许多基本问题,您应该在进行之前清除这些问题。请参阅以下代码中的评论:
Sub Matrix()
' declare ALL your variables
Dim n As Long, m As Long
Dim ws As Worksheet
' specify type for all variables, otherwise they will be Variant
Dim Sigmai As Single, Sigmaj As Single, Rho As Single
' Explicitly reference the required sheet
Set ws = Sheet1 ' or ActiveSheet or whatever
' qualify range references with worksheet
Sigmai = ws.Range("b12").Value
Sigmaj = ws.Range("b13").Value
Rho = ws.Range("b14").Value
' Specify required lower bound. Default base is 0
Dim matrixelement(1 To 10, 1 To 10) As Single
For n = 1 To 10
For m = 1 To 10
matrixelement(n, m) = Sigmai * Sigmaj * Rho
Next m
Next n
' return result to sheet in one go
ws.Range("A1:J10") = matrixelement
End Sub
对于
我想要做的最后一件事就是将该矩阵乘以该矩阵的转置
部分,我不确定你到底是什么,但MMULT
可能有用,例如
ws.Range("L1:U10") = Application.WorksheetFunction.MMult(matrixelement, matrixelement)
答案 1 :(得分:0)
你可以直接传递数组并将结果直接放到工作表中。一个工作表,如果实际上只是一个大的二维数组。
对Chris Neilsen的代码进行自我修复:
Sub Matrix()
' declare ALL your variables
Dim n As Long, m As Long
Dim ws As Excel.Worksheet
' specify type for all variables, otherwise they will be Variant
Dim Sigmai As Single, Sigmaj As Single, Rho As Single
' Explicitly reference the required sheet
Set ws = Sheet1 ' or ActiveSheet or whatever
' qualify range references with worksheet
Sigmai = ws.Range("b12").Value
Sigmaj = ws.Range("b13").Value
Rho = ws.Range("b14").Value
For n = 1 To 10
For m = 1 To 10
ws.cells(n,m).value = Sigmai * Sigmaj * Rho
Next m
Next n
End Sub
我不明白的一些事情: