UBound function does not work

B

BEETAL

I have defined two matrices InMatrix1 and InMatrix2 on a spredsheet by names.
The programme does not execute.
When I type UBound in the VBEditor, there is no prompt for the UBound word
after I press shift+spacebar.

What is the problem?
Function MatrixMult(InMatrix1 As Variant, InMatrix2 As Variant) As Variant

'generic matrix multiplication engine, pure BASIC code
Dim OutMatrix()
Dim i As Integer, k As Integer
Dim j As Integer
Dim Sum As Integer
ReDim OutMatrix(1 To UBound(InMatrix1, 1), 1 To UBound(InMatrix2, 2))

Application.Volatile 'force recalculation with spreadsheet

For i = 1 To UBound(InMatrix1, 1) 'rows of inmatrix1
For j = 1 To UBound(InMatrix2, 2) 'cols of inmatrix2
Sum = 0
For k = 1 To UBound(InMatrix1, 2)
Sum = Sum + InMatrix1(i, k) * InMatrix2(k, j)
Next
OutMatrix(i, j) = Sum
Next
Next

MatrixMult = OutMatrix

End Function
 
B

BEETAL

I forgot to address the question to the Experts.
Dear Experts, Please help.

Thank you so much for taking time to read the post. More thanks for
solutions,in advance.
 
P

Per Jessen

Hi

You can not pass two arrays to an UDF, so I have changed it to two ranges,
where the values will be processed.

I'm not sure what you expect to get as output. A function will only return
one value. As the function is working now it will return the value of the
first element in "OutMatrix".

Hopes it helps.

Public Function MatrixMult(InMatrix1 As Range, InMatrix2 As Range) As
Variant

'generic matrix multiplication engine, pure BASIC code
Dim OutMatrix()
Dim i As Integer, k As Integer
Dim j As Integer
Dim Sum As Integer
ReDim OutMatrix(1 To InMatrix1.Rows.Count, 1 To InMatrix2.Columns.Count)

Application.Volatile 'force recalculation with spreadsheet

For i = 1 To InMatrix1.Rows.Count 'rows of inmatrix1
For j = 1 To InMatrix2.Columns.Count 'cols of inmatrix2
Sum = 0
For k = 1 To InMatrix1.Columns.Count
Sum = Sum + InMatrix1.Cells(i, k) * InMatrix2.Cells(k, j)
Next
OutMatrix(i, j) = Sum
Next
Next

MatrixMult = OutMatrix

End Function

Best regards,
Per
 
B

BEETAL

Dear Per Jessen,

tell you something! You must be a highly intelligent programmer. am i right
or not?

Thank You ,Sir.

as of now ,it works. I have hundreds of lines to pro gramme and debug. I
shall always expect to draw your kind attention to my doubts,hurdles(small
and big).

thanking you again,

with best regards

Siddhartha
 
P

Per Jessen

Hi Siddhartha

Thanks for your reply. I'm glad to help.

BTW: I'm not a programmer, I've just picked up a little knowledge of VBA
programming.

Best regards,
Per
 
D

Dana DeLouis

As a side note, are you aware of the function MMult?

Sub Demo()
Dim m, m1, m2
m1 = [{1,2;3,4;5,6}]
m2 = [{11,12,13;14,15,16}]

With WorksheetFunction
m = .MMult(m1, m2)
End With
End Sub

HTH
Dana DeLouis
 

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