# Matrix multiplication code

The following code is to illustrate the matrix multiplication method mentioned previously. For simplicity sake, I’m limiting the size of the matrices to 3.

```const int cnSize = 3;
int[,] A = new int[cnSize, cnSize];
int[,] B = new int[cnSize, cnSize];
int[,] C = new int[cnSize, cnSize];
int[] x = new int[cnSize];
int[] y = new int[cnSize];
Random rand = new Random();
int i, j, k;

// fill matrix and vector with random values
for (i = 0; i < cnSize; ++i)
{
for (j = 0; j < cnSize; ++j)
{
A[i, j] = rand.Next(1, 10);
B[i, j] = rand.Next(1, 10);
}
x[i] = rand.Next(1, 10);
}

// matrix-vector multiplication
for (i = 0; i < cnSize; ++i)
{
y[i] = 0;
for (k = 0; k < cnSize; ++k)
{
y[i] += A[i, k] * x[k];
}
}

// matrix-matrix multiplication
for (i = 0; i < cnSize; ++i)
{
for (j = 0; j < cnSize; ++j)
{
C[i, j] = 0;
for (k = 0; k < cnSize; ++k)
{
C[i, j] += A[i, k] * B[k, j];
}
}
}

Console.WriteLine("Matrix-vector multiplication");
for (i = 0; i < cnSize; ++i)
{
Console.Write("[");
for (j = 0; j < cnSize; ++j)
{
}
Console.WriteLine("][{0}] {1} [{2}]", x[i].ToString().PadLeft(3), ((cnSize / 2) == i ? "=" : " "), y[i].ToString().PadLeft(3));
}
Console.WriteLine();

Console.WriteLine("Matrix-matrix multiplication");
for (i = 0; i < cnSize; ++i)
{
Console.Write("[");
for (j = 0; j < cnSize; ++j)
{
}
Console.Write("][");
for (j = 0; j < cnSize; ++j)
{
}
Console.Write("] {0} [", ((cnSize / 2) == i ? "=" : " "));
for (j = 0; j < cnSize; ++j)
{
}
Console.WriteLine("]");
}
Console.WriteLine();
```

You will notice there's a lot of nested `for` loops. Get used to it. Here's a screenshot of the output: Exercise: Explain what this does. (a ternary operator refresher might help)

```((cnSize / 2) == i ? "=" : " ")
```
1. Will Dwinnell

This is one thing to like about MATLAB. Matrix multiplication is *easy*! With 2 matrices, matrix multiplication is as easy as:

C = A * B;

As we say: “Life is too short for DO loops.”

2. Vincent Tan

@Will: C = A * B;

Hey, that’s cheating!! *Real* programmers use loops. *smile*

3. haymo

sir can u send me the coding of matrix multiplication

4. apit

what about matrix multiplication using systolic array method?..

i have problem in shifting the same value from one cell to another while doing the multiplication and addition operation simultaneously.

5. Vincent

Hi apit, if I understand that method correctly, you don’t actually shift the same value from one cell to another. That sort of defeats the purpose.

You are shifting the inputs into the cell, and doing addition and multiplication at the same time.

http://web.cecs.pdx.edu/~mperkows/temp/May22/0020.Matrix-multiplication-systolic.pdf

6. Vincent

You know, that question actually look like homework…

7. apit

What types of operation i need to use for shifting the inputs into the cell, and doing addition and multiplication at the same time, for loop/while loop or multithreading?

8. Vincent

If I understand it correctly, the term “shift” is misleading. It’s not the binary shift. It’s just an assignment.

For example, in the link I gave, when T=1, say
SA1 = a(0,0) * b(0,0)

At T=2,
SA1 += a(0,1) * b(1,0)

At this point, SA1 contains a(0,0) * b(0,0) + a(0,1) * b(1,0).

I believe this is what it means by doing addition and multiplication at the same time.

As for the loop conditions, my guess is that the number of iterations for an N by N matrix multiplication is N + 2 + (N-1)

9. apit

how to make the timing 2N?, where the timing for systolic array multiplication is 2N that is the multiplication is more complex than conventional method.

10. Vincent

apit, I’m probably wrong on the number of iterations. I looked at the link you gave, and it says 3*N – 3.

For a 10 by 10 matrix, that comes up to be 27.

Would it be better if you told me what you know, and what the actual question is? Then I might be able to better help you.

11. apit

I want to ask about systolic array matirx multiplication. for example, if we have three cells. A=2, B=3, C=4. I want to multiply them with 7,8,9. first I would times 2 with 7, next i pass the value of 7 to cell B to be multiply but at the same time the cell A received the value of 8 and it will be then multiplied by 2. then the process goes on. how would I do that?

12. Vincent

I’m sorry I can’t explain it better. Perhaps this PowerPoint presentation can do it better:

http://www.cs.ucf.edu/courses/cot4810/fall04/presentations/Systolic_Arrays.ppt

The trick I believe, is in manipulating the array index, and that there is a separate variable belong to each array cell (or processor register) that stores the intermediate result.

I would imagine say the top-left cell to take in
A[0,0]*B[0,0] at T=1 (for example).

Then at T=2, it takes in A[0,1]*B[1,0] and adds it to its intermediate (register) value.

At T=3, it takes in A[0,2]*B[2,0].

So the general formula for the top-left cell is
A[0,T-1]*B[T-1,0] at time T

The thing is, the formula is different for each cell. So you’ll have to figure it out. Just write down all the input index for each cell, and find a pattern regarding the indices from the input.

There will be edge conditions, such as what happens when T=0 for an index T-1? You don’t do anything when that happens.

13. apit

what about their right side cell..what its formula..the example

14. apit

From the slide, what the code for flip the column of matrix A and flip the row of matrix B.

The slide shown the 3×3 size matrix. What about the 5×5 size matrix. How to flip the 2 column and the 2 row of matrix A and matrix B.

15. apit

tq..i have got all the general formula for each cell.

i have the question about the example of the coding to flip the matrix A = 1 2 3 4 5
1 2 4 5 6
3 4 5 6 7
4 3 2 1 4
3 4 2 4 5

to be matrix A = 5 4 3 2 1
6 5 4 2 1
7 6 5 4 3
4 1 2 3 4
5 4 2 4 3

16. Vincent

apit, it’s a simple swapping of values. Iterate through each row of A, then swap A[i,0] with A[i,4], and A[i,1] with A[i,3].

17. apit

i got the example of swap operations for data a and b..

#include
#include

void swap(int *i, int *j)
{
int t;
t = *i;
*i = *j;
*j = t;
}

void main()
{
int a,b;
a=5;
b=10;
printf(“%d %dn”,a,b);
swap(&a,&b);
printf(“%d %dn”,a,b);

getch();
return 0;
}

what about the first column of matrix A swap with last column of matrix A?..

18. apit

// swapping matrix.cpp : Defines the entry point for the console application.
//

#include
#include
#include
#include
using namespace std;

void swapping(int **Matrix,int Choose_Rows1,int Choose_Rows2,int Number_columns)
{
int temp=0;
for(int i=0; i<Number_columns; i++)
{
temp=Matrix[Choose_Rows1][i];
Matrix[Choose_Rows1][i]=Matrix[Choose_Rows2][i];
Matrix[Choose_Rows2][i]=temp;
}
}

int main(int argc, char* argv[])
{
int Number_Rows;
int Number_columns;
int **Matrix;

cout<<"Please enter Number of Rows : "<>Number_Rows;
cout<<endl;

cout<<"Please enter the Number of columns : "<>Number_columns;
cout<<endl;

Matrix=new int*[Number_Rows];
for(int i=0; i<Number_columns; i++)
{
Matrix[i]=new int [Number_columns];
}
cout<<endl<<"enter Matrix :"<<endl;
for(int i=0; i<Number_Rows; i++)
{
for(int j=0; j>Matrix[i][j];
}

}
cout<<endl;

int Choose_Rows1;
int Choose_Rows2;

cout<<"Choose_Rows1 : "<>Choose_Rows1;
cout<<endl;

cout<<"Choose_Rows2 : "<>Choose_Rows2;
cout<<endl;
while(Choose_Rows1=0&&Choose_Rows2=0)
{
swapping(Matrix,Choose_Rows1,Choose_Rows2,Number_columns);
cout<<endl;
for(int i=0; i<Number_Rows; i++)
{
for(int j=0; j<Number_columns; j++)
{
cout<<Matrix[i][j];
cout<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<"Choose_Rows1 : "<>Choose_Rows1;
cout<<endl;

cout<<"Choose_Rows2 : "<>Choose_Rows2;
cout<<endl;
}
cout<<"Press Any Key To End";
getch();
return 0;
}

this code are for choose_row1 and choose_row2 operation..can u upgrade this code to add the choose_row3 and choose_row4..

19. Vincent

apit, first you want to swap columns. Now you want to swap rows. I’m confused.

Next, choose_row1 and choose_row2 are variables.

choose_row1 = 3;
choose_row2 = 4;

And you’ll be swapping rows 3 and 4.

I also think I’m the wrong person to ask. A programming forum might be more helpful for you, because I believe you’re still struggling with the basics of programming logic. You’ll get faster help, and more information on what you intend to achieve.