Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Friday, August 19, 2016

xlMatrix series: Subtracting 2 matrixes

Report any errors to afstblogs@gmail.com









Function xlMSUB(M1, M2)

    Dim rslt()
    Dim i, j
    Dim rws1, rws2, cls1, cls2
   
    rws1 = M1.Rows.Count
    cls1 = M1.Columns.Count
    rws2 = M2.Rows.Count
    cls2 = M2.Columns.Count
    ReDim rslt(1 To rws1, 1 To cls1)
   
    If (rws1 <> rws2) Or (cls1 <> cls2) Then
        xlMSUB = "xlERR1"
    Else
        For i = 1 To rws1
            For j = 1 To cls1
                rslt(i, j) = M1(i, j) - M2(i, j)
            Next j
        Next i
        xlMSUB = rslt
    End If
        
End Function

xlMatrix series: xlMADD - summing 2 matrixes

Report any errors to afstblogs@gmail.com







Function xlMADD(M1, M2)

    Dim rslt()
    Dim i, j
    Dim rws1, rws2, cls1, cls2
   
    rws1 = M1.Rows.Count
    cls1 = M1.Columns.Count
    rws2 = M2.Rows.Count
    cls2 = M2.Columns.Count
    ReDim rslt(1 To rws1, 1 To cls1)
   
    If (rws1 <> rws2) Or (cls1 <> cls2) Then
        xlMADD = "xlERR"
    Else
        For i = 1 To rws1
            For j = 1 To cls1
                rslt(i, j) = M1(i, j) + M2(i, j)
            Next j
        Next i
        xlMADD = rslt
    End If
        
End Function

Wednesday, June 1, 2016

Sample covariance matrix






Function xlCovSMtx(rng As Range)
   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                mtx(i, j) = .Covariance_S(rng.Columns(i), rng.Columns(j))
            Next j
        Next i
   
        xlCovSMtx = mtx
   
    End With

End Function

Function xlCovSMtx_U(rng As Range)
   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                If i <= j Then
                    mtx(i, j) = .Covariance_S(rng.Columns(i), rng.Columns(j))
                Else
                    mtx(i, j) = 0
                End If
            Next j
        Next i
   
        xlCovSMtx_U = mtx
   
    End With

End Function

Function xlCovSMtx_L(rng As Range)
   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                If i >= j Then
                    mtx(i, j) = .Covariance_S(rng.Columns(i), rng.Columns(j))
                Else
                    mtx(i, j) = 0
                End If
            Next j
        Next i
   
        xlCovSMtx_L = mtx
   
    End With

End Function


Sunday, May 29, 2016

Correlation matrix




   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                mtx(i, j) = .Correl(rng.Columns(i), rng.Columns(j))
            Next j
        Next i
   
        xlCorrMtx = mtx
   
    End With

End Function

Function xlCorrMtx_U(rng As Range)
   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                If i <= j Then
                    mtx(i, j) = .Correl(rng.Columns(i), rng.Columns(j))
                Else
                    mtx(i, j) = 0
                End If
            Next j
        Next i
   
        xlCorrMtx_U = mtx
   
    End With

End Function

Function xlCorrMtx_L(rng As Range)
   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                If i >= j Then
                    mtx(i, j) = .Correl(rng.Columns(i), rng.Columns(j))
                Else
                    mtx(i, j) = 0
                End If
            Next j
        Next i
   
        xlCorrMtx_L = mtx
   
    End With

End Function