Adding matrices

Joined
Jun 7, 2010
Messages
1
Reaction score
0
Hi everybody,

function (MSUM(Array1, Array2) ) adds two matrices. Everything works well when inputs are ranges from the sheet. (e.g. MSUM(A1:B2,A1:B2))

But when I input functions inside, it returns #Value. (e.g. MSUM(Transpose(A1:B2),Transpose(A1:B2)) )

Please help people!

Here is the code:

Function MSUM(Array1 As Variant, Array2 As Variant)

Dim n As Integer, m As Integer, u As Integer, v As Integer
Dim i As Integer, j As Integer
Dim Ans() As Variant

'Figuring out dimensions of the Array1 and Array2. UBound, LBound for some reason do not work for me properly

For i = 1 To 30
For j = 1 To 30
If Array1(1, j) = 0 And Array2(1, j) = 0 Then
m = j - 1 ' # of columns
v = j - 1
Exit For
End If
Next j
If Array1(i, 1) = 0 And Array2(i, 1) = 0 Then
n = i - 1 ' # of rows
u = i - 1
Exit For
End If
Next i

ReDim Ans(1 To n, 1 To m)

'Getting result
For i = 1 To n
For j = 1 To m
Ans(i, j) = Array1(i, j) + Array2(i, j)
Next j
Next i

MSUM = Ans
End Function

Regards,
Marik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top