 Hello friends, I am Sanjay Bhukta. I welcome you on Sanjay Bhukta Tech School. In this video I am going to tell you how you can write algorithm for matrix multiplication. So I am starting the algorithm. So the name of algorithm is multiplication. Here we will require three methods A, B and C. So we will be multiplying A with B and the result will be stored in C matrix. Then I have R1, C1, R2 and C2. So R1 and C1 are rows and columns of course matrix. R2 and C2 are rows and columns of second matrix. So whenever this algorithm will be implemented to a random matrix. So we will be implementing a function. So in that function we will require these parameters. So if you want to write description related to these parameters, you can write those here as well. But I am just starting the steps because I already explained what each parameter is. So now step number one is start and step number two. So here we need to check out this condition. So that this condition will be check in better. Two matrix can be multiplied or not. So let's say size of A matrix is 3 by 3 and size of B matrix is also 3 by 3. So in this case matrix multiplication is possible because rows and columns for both the matrices are equal. Now if this matrix has this size and B matrix has size 3 by 4. So A is having connection 2 by 3 and B is having connection 3 by 4. So we need to check these things. Column value of first matrix should be equal to row value of second matrix. If this is equal then you can apply matrix multiplication. But if case is this, so in this case we cannot multiply the matrices. So here we need to check column value of first matrix with row value of second matrix. If both are unequal, if C1 is not equal to R2. So if this is the case then we cannot perform the matrix multiplication. So here we can write then and with that I will print multiplication possible and exit. So in your program why you will be converting this algorithm with any programming language. So in that case we need to make sure if both are unequal then you need to terminate execution of your pattern. So that hung necessarily matrix multiplication will not be performed. After checking this condition now we can write loops so that multiplication can be performed. So if this condition is false like C1 and R2 both are unequal it means we can apply the matrix multiplication. So now I am starting first loop so repeat for I equals to 1, 2. So first loop will be repeating row times of first matrix so that means R1. So R1 is row for first matrix so I am repeating our first matrix R then again repeat for J equals to 1, 2 column. So C2 is for column of second matrix. So that is used there. Now I am going to use third matrix IG position and I am assigning 0 to that position because this position will be scoring the matrix multiplication result. After this I am writing one more matrix for K equals to 1, 2. So here we need to write a particular value so it can be this or it can be this. So you can say it can be C1 or it can be R2. So I am putting C1 because whenever we choose to multiply to matrix so we need to multiply a row with column. So if first matrix column size is equal to or second matrix row size then this multiplication will happen so you can use any particular value from here for iterating this column. So after writing this complete algorithm I will be taking one and come back so that you can understand how this algorithm will be working. So after writing this here you can write C of IJ equals to C of IJ plus A of IK multiplied by B of KG. So this is the multiplication statement that will be multiplying your matrix and the result will be 4 into C matrix. Now we need to stop all the loops so after this we will write A of U. So it is for this then you can write A of U for this and then A of U for this. So then by implementing or converting this algorithm with any programming method you will remember that we are going to terminate them because they are going to do schedule basis. So the algorithm we don't do schedule basis but while writing program for this algorithm you will do schedule basis. So that's why this end of loop will be important. And after completion of this you can write standard mode that is because your result is already up in C matrix so if you want to print this matrix in this algorithm so after completion of these loops you can print but if you want to do any other calculation then you can do that. So that depends upon how you will be converting your algorithm in any programming method. So now let's understand this algorithm so that you can understand the statements or the steps which are here. So let's say A matrix is 2 by 3 dimension and B matrix is of 3 by 4 dimension. So first I am drawing A this is B. So what will happen in case of multiplication? This location will be multiplied with this, this location with this, this location with this. And all the modifications will be stored into C matrix. So now we need to figure out what will be the direction of C matrix. So that will be equivalent to this 2 by 4. So 2 rows and 4 columns. So this row will be multiplied with this column and the result will be stored here. Then again this row will be multiplied with this column and the result will be stored here. this over here then again this row with this column and result will be stored here then again this row with this column and the result will be stored here right so here you can see this is r1 this is c1 this is r2 and this is c2 right and now here you can put the right one so this is 2 c2 is 4 and here we have mentioned c1 so either you can use c1 directly or r2 that is also 3 and here we also check the condition c1 not equals to r2 so in this case c1 and r2 both are equal so this will if both are equal then your algorithm will be here so here you can write now you can iterate the loops and the loop will be having multiplication that you can see so I am just iterating it one time so that you can expand the logic so let's say 1, 2, 3 is available here and 4, 5, 6, 7, 8 are available here so I am going to multiply this row with this column so i is 1 so initially i is 1 j is also 1 so i is 1 then j is 1 then assign you know if you see 1, 1 position so c1, 1 position is this so it is having 0 right now then we need to iterate this k loop so k loop will be 3 times and 3 times these statements will be performed so c0, 0 will be added with a, i, k into b, k, j so this time k is also 1 so all i, j, k are 1, 1 so a, 1, 1 will be multiplied by p, 1, 1 so it is 1, 1 position and it is 1, 1, 1 position so 1 into 4 is 4 that will be added to this position right so now i hope you understood why we put 0 because we are adding some value to a particular position so we need to initialize it with 0 otherwise garbage values will be added and the incorrect results will be correct then only k will be implemented so this time value of k is 2 so again perform this operation so earlier it was 1, 1 now it is 1, 2 and 2 work so 1, 2 position will be multiplied by 2, 1 so 1, 2 is for a that is this and 2, 1 is for b that is this so 1 will be multiplied with 2 that is 10 and that will be added with this c1, 1 position so it is 4 so it will become 14 then again a will be implemented so k is 3 so this time 1, 3 and 3, 1 position will be so 1, 3 is this and 3, 1 is this so 3 will be multiplied with 6 so 3 into 6 18 plus 14 it will become correct so here you can see a repeated 3 times so first 4 is multiplied with our first column of this matrix now k2 will be there so j will be implemented now because this is our formation of a structure so j will be 2 then again this will be initialized with 0 and k again will start from 1 so this way this time this row will be multiplied with this second column and the result will be this row here at this position so this way i hope you understood how we can implement or we can write algorithm for matrix multiplication and if rows and columns are same then also you will work if rows and columns are different for new matrix then this if condition will validate rather we can multiply matrix or what and accordingly your results will be generated so i hope you understood how we can write algorithm for matrix multiplication if you want to watch its implementation you can see so you can go to description of this video then you will find links of playlist so you can follow that and you can learn how we can convert algorithm with c-language book so i hope you understood whatever i explained in this video thank you for watching this video