View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
[email protected] costanza99g@gmail.com is offline
external usenet poster
 
Posts: 2
Default Subtract all individual elements of two matrix

By mistake I typed the name of one variable incorrectly:

here is the revised post:
Hi,

I'm trying to subtract all the individual elements of two matrices.

One of the matrices is calculated using this function:

Function CovarianceMatrix(rng As Range) As Variant

Dim i As Integer
Dim j As Integer
Dim colnum As Integer
Dim rownum As Integer
Dim auxCovarianceMatrix() As Variant


colnum = rng.Columns.Count
' covariance matrix
ReDim auxCovarianceMatrix(colnum - 1, colnum - 1)

For i = 1 To colnum
For j = 1 To colnum
auxCovarianceMatrix(i - 1, j - 1) = Application.WorksheetFunction.Covar(rng.Columns(i) , rng.Columns(j))
Next j
Next i

CovarianceMatrix = auxCovarianceMatrix
End Function

the other matrix (named averagecovariancematrix) is calculated using a very similar formula.

I verified that both of these functions work well in the worksheet.

Now I need to subtract all the individual elements of both matrices (basic matrix operation), but I'm getting errors and I don't understand why.
I created a new function that calls the function I posted above and the other one.

This is the function I'm using:
Function AddMAtrix(rng as range) as variant

Aux1=CovarianceMatrix(rng)
Aux2=AverageCovarianceMatrix(rng)

AddMatrix=Aux1-Aux2

end function

thanks






On Sunday, September 30, 2012 2:22:23 AM UTC+1, wrote:
Hi,



I'm trying to subtract all the individual elements of two matrices.



One of the matrices is calculated using this function:



Function CovarianceMatrix(rng As Range) As Variant



Dim i As Integer

Dim j As Integer

Dim colnum As Integer

Dim rownum As Integer

Dim auxCovarianceMatrix() As Variant





colnum = rng.Columns.Count

' covariance matrix

ReDim auxCovarianceMatrix(colnum - 1, colnum - 1)



For i = 1 To colnum

For j = 1 To colnum

auxCovarianceMatrix(i - 1, j - 1) = Application.WorksheetFunction.Covar(rng.Columns(i) , rng.Columns(j))

Next j

Next i



CovarianceMatrix = auxCovarianceMatrix

End Function



the other matrix (named avcovariancematrix) is calculated using a very similar formula.



I verified that both of these functions work well in the worksheet.



Now I need to subtract all the individual elements of both matrices (basic matrix operation), but I'm getting errors and I don't understand why.

I created a new function that calls the function I posted above and the other one.



This is the function I'm using:

Function AddMAtrix(rng as range) as variant



Aux1=CovarianceMatrix(rng)

Aux2=AverageCovarianceMatrix(rng)



AddMatrix=Aux1-Aux2



end function



thanks