Matrix multiplication

This code shows how to multiply two matrices of size 3 X 3.


Private Sub MatrixMultiplication()
Dim strArray As String = ""
'Create three matrices.
Dim M1(3, 3), M2(3, 3), M3(3, 3) As Integer
Dim intValue As Integer = 10
Dim intI, intJ As Integer

'intI represents row and
'intJ represents column of all the matrices.
'The intI in the for loop considers
'all the rows and intJ considers all
'the columns.So row is considered first
'and then the column, while performing
'every operation over matrix.
'Add data in first matrix M1.
'Here the first element is 10 and
'then it is incremented by 10.
For intI = 0 To 2
For intJ = 0 To 2
'Value is saved in each cell of matrix
M1(intI, intJ) = intValue
intValue = intValue + 10
Next
Next
'Add data in second matrix M2.
For intI = 0 To 2
For intJ = 0 To 2
'Value is saved in each cell of matrix
M2(intI, intJ) = intValue
intValue = intValue + 10
Next
Next
'Display the first matrix M1.
For intI = 0 To 2
For intJ = 0 To 2
If strArray.Length = 0 Then
strArray = " " & M1(intI, intJ)
Else
strArray = strArray & " " & M1(intI, intJ)
End If
Next
'New line is added after row is created.
strArray = strArray & vbNewLine
Next
MessageBox.Show("The first matrix is " & vbNewLine & strArray)
'Display the second matrix M2.
strArray = ""
For intI = 0 To 2
For intJ = 0 To 2
If strArray.Length = 0 Then
strArray = " " & M2(intI, intJ)
Else
strArray = strArray & " " & M2(intI, intJ)
End If
Next
'New line is added after row is created.
strArray = strArray & vbNewLine
Next
MessageBox.Show("The second matrix is " & vbNewLine & strArray)
'Third matrix M3 is resultant of the multiplication of
'two matrices M1 and M2.
For intI = 0 To 2
For intJ = 0 To 2
'The multiplication is saved in corresponding cell
'of matrix M3.
M3(intI, intJ) = M1(intI, intJ) * M2(intI, intJ)
Next
Next
'Display the third matrix M3.
strArray = ""
For intI = 0 To 2
For intJ = 0 To 2
If strArray.Length = 0 Then
strArray = " " & M3(intI, intJ)
Else
strArray = strArray & " " & M3(intI, intJ)
End If
Next
'New line is added after row is created.
strArray = strArray & vbNewLine
Next
MessageBox.Show("The multiplication of matrices is " & vbNewLine & strArray)
End Sub


Comments

Author: siva03 Dec 2009 Member Level: Gold   Points : 0

good work

Author: siva03 Dec 2009 Member Level: Gold   Points : 2

c# version of this code
private void button1_Click(object sender, EventArgs e)
{
string strArray="";
int[,] m1 = new int[3, 3];
int[,] m2 = new int[3, 3];
int[,] m3 = new int[3, 3];
int intValue = 10;
int intI, intJ;
for (intI = 0; intI <= 2; intI++)
{
for (intJ = 0; intJ <= 2; intJ++)
{
m1[intI, intJ] = intValue;
intValue = intValue + 10;
}
}
for (intI = 0; intI <= 2; intI++)
{
for (intJ = 0; intJ <= 2; intJ++)
{
m2[intI, intJ] = intValue;
intValue = intValue + 10;
}
}
for (intI = 0; intI <= 2; intI++)
{
for (intJ = 0; intJ <= 2; intJ++)
{
if (strArray.Length == 0)
{
strArray = " " + m1[intI, intJ];
}
else
{
strArray = strArray +" " + m1[intI, intJ];
}
}
strArray = strArray + "\n";
}

MessageBox.Show("the m1 is \n" + strArray);
strArray = "";
for (intI = 0; intI <= 2; intI++)
{
for (intJ = 0; intJ <= 2; intJ++)
{
if (strArray.Length == 0)
{
strArray = " " + m2[intI, intJ];
}
else
{
strArray = strArray + " " + m2[intI, intJ];
}
}
strArray = strArray + "\n";
}

MessageBox.Show("the m2 is \n" + strArray);
for (intI = 0; intI <= 2; intI++)
{
for (intJ = 0; intJ <= 2; intJ++)
{
m3[intI, intJ] = m1[intI, intJ] * m2[intI, intJ];
}
}
strArray="";
for (intI = 0; intI <= 2; intI++)
{
for (intJ = 0; intJ <= 2; intJ++)
{
if (strArray.Length == 0)
{
strArray = " " + m3[intI, intJ];
}
else
{
strArray = strArray + " " + m3[intI, intJ];
}
}
strArray = strArray + "\n";

}MessageBox.Show("the Result is \n" + strArray);
}

Guest Author: vino05 Aug 2012

Dim strArray As String = ""
'Create three matrices.
Dim M1(3, 3), M2(3, 3), M3(3, 3) As Integer
Dim intValue As Integer = 10
Dim intI, intJ, intK As Integer

'intI represents row and
'intJ represents column of all the matrices.
'The intI in the for loop considers
'all the rows and intJ considers all
'the columns.So row is considered first
'and then the column, while performing
'every operation over matrix.
'Add data in first matrix M1.
'Here the first element is 10 and
'then it is incremented by 10.
For intI = 0 To 2
For intJ = 0 To 2
'Value is saved in each cell of matrix
M1(intI, intJ) = InputBox("ENER THE ELEMENNTS OF 1st MATRIX") 'intValue
'intValue = intValue + 10
Next
Next

'Add data in second matrix M2.
For intI = 0 To 2
For intJ = 0 To 2
'Value is saved in each cell of matrix
M2(intI, intJ) = InputBox("ENER THE ELEMENNTS OF 2nd MATRIX") 'intValue
'intValue = intValue + 10
Next
Next

'Display the first matrix M1.
For intI = 0 To 2
For intJ = 0 To 2
If strArray.Length = 0 Then
strArray = " " & M1(intI, intJ)
Else
strArray = strArray & " " & M1(intI, intJ)
End If
Next
'New line is added after row is created.
strArray = strArray & vbNewLine
Next
MessageBox.Show("The first matrix is " & vbNewLine & strArray)

'Display the second matrix M2.
strArray = ""
For intI = 0 To 2
For intJ = 0 To 2
If strArray.Length = 0 Then
strArray = " " & M2(intI, intJ)
Else
strArray = strArray & " " & M2(intI, intJ)
End If
Next
'New line is added after row is created.
strArray = strArray & vbNewLine
Next
MessageBox.Show("The second matrix is " & vbNewLine & strArray)

'Third matrix M3 is resultant of the multiplication of
'two matrices M1 and M2.
For intI = 0 To 2
For intJ = 0 To 2
M3(intI, intJ) = 0
For intK = 0 To 2
'The multiplication is saved in corresponding cell
'of matrix M3.
M3(intI, intJ) = M3(intI, intJ) + (M1(intI, intK) * M2(intK, intJ))
Next
Next
Next

'Display the third matrix M3.
strArray = ""
For intI = 0 To 2
For intJ = 0 To 2
If strArray.Length = 0 Then
strArray = " " & M3(intI, intJ)
Else
strArray = strArray & " " & M3(intI, intJ)
End If
Next
'New line is added after row is created.
strArray = strArray & vbNewLine
Next
MessageBox.Show("The multiplication of matrices is " & vbNewLine & strArray)

Author: Phagu Mahato06 Oct 2013 Member Level: Gold   Points : 0

Thank It is really an amazing Programming code provided by Programmer and good responses of Shiva. You can also try below code snippet for Matrix Multiply;
using System;
using System.Data;
using System.Collections;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;

public class ScriptMain : UserComponent
{
public override void PreExecute()
{
base.PreMatrixExecute();

}

public override void PostMatrixExecute()
{
base.PostMatrixExecute();

}

public override void Input_ProcessInputRow(Input0Buffer Row)
{



float[,] CreateArray = new float[4, 4];
int Rnum;
int Colnum;
int MatrixSize = Row.MatrixSize;
Rnum = Row.MatrixRow;
Colnum = Row.MatrixColumn;


CreateArray[Rnum, Colnum] = Row.AverageTransition;
CreateArray.SetValue(Row.AverageTransition, Rnum, Colnum);


float[,] constantArray = CreateArray;



OutputBuffer.AddRow();
OutputBuffer.FromState = Row.FromSize;
OutputBuffer.ToState = Row.ToSize;
OutputBuffer.MatrixColumn = Row.MatrixColumn;
OutputBuffer.MatrixRow = Row.MatrixRow;
OutputBuffer.TransitionProbability = CreateArray[Rnum, Colnum];


}

}

Continue your Responses ;



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: