 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how you can multiply two matrix in C language. So before starting, I just want to share one information. If you go to detail or description of this video, you will find links of various playlist related to C programming. So you can follow them to learn programming. And now I'm going to explain the logic first, like how matrix will be multiplied. So we will take mathematical example first and then I will explain a C related pool so that you can get to know how matrix are multiplied in C language. So first time creating three matrix. So let's say this is first matrix whose name is A. These are its indexes. And I'm putting one as all. All indexes having value as one. Then this is second matrix. It is also having values as one. And its name is B. Now I'm going to draw third matrix. So this is matrix C in which we need to store all the output values. So the multiplication result will be stored inside this matrix. Now if we talk about matrix multiplication in mathematics. So what we do, we first pick this first row of first matrix and we multiply it with first column of second matrix. So it means these three ones will be multiplied with these three ones and their multiplications are added. So what will happen one into one plus one into one plus one into one. So this is multiplication of our first one and this one. Then this second multiplication is for this one and this one. This multiplication is for this one and this one. So this way first row and first column. So first row first matrix and first column of second matrix are multiplied and their multiplications are added. So finally they will make a result as three. This three will store here. Right now next time we need to pick this first row and we need to multiply it with second column and again result will be three. So it will be stored here. Then again first row of first matrix will be multiplied with third column of second matrix. So it will be three. So this way it's first row will be filled. Then again second row of first matrix will be multiplied three times first with first column then second column then third column. So three different values will be available here. So this way we multiply matrix and mathematics. So now I'm going to write a code so that through the program we will be able to identify this multiplication. So now I'm going to write that code. So I assume that you can easily read values of A and B. So I'm not writing that code which is responsible for reading through matrix. I'm just writing the multiplication code. So this is the first loop which will repeat three times. Then second loop which will also repeat three times. So here you can see two for loops are available. Now inside this second loop I'm going to start third loop. But before that I'm writing C of IJ equals to zero. So C matrix IJ index will be initialized with zero. Then I'm starting third loop which will also repeat three times. And inside this loop I'm going to write the statement which will be responsible for multiplication and addition of the. So I'm writing C of IJ equals to C of IJ plus A of IK multiplied by B of KJ. Multiplied by B of KJ. Right. And then I'm closing all these. So these are the steps which are responsible for matrix multiplication. So these three things we need to identify step by step so that you will be able to multiply two matrix. So now I'm starting from the beginning. So initially you can see I is zero. So I'm putting it here. I is zero. Then we will come to this loop J is zero. So there are three necessary two. One, two, three. So when I is zero control will be transferred to J. So it will repeat three times. When J is zero control will be transferred to K. So K will repeat three times. So when I is zero and J is zero. So K will repeat three times. Then we just need to increase J. Again K will repeat three times. Right. So before starting game here C IJ equals to zero is natural. So I will be moving these values and C index zero zero or C matrix zero zero index will be having value as zero. Now just focus on this loop. Just focus on this loop and it will repeat three times. It will repeat three times and try to maintain these indexes. So here you can remember I and J both are zero. So I will be zero three times. And here J will be zero three times. Right. Now K is zero initially. So here you can see zero zero are available. So C zero zero will be added with I K. So K is zero. So this will become a zero zero. Here also K is zero. So it is also zero zero. So A zero zero position will be multiplied with B zero zero position. So A zero zero position is this B zero zero position is this both will be multiplied and they will be added to C IJ. And C IJ means zero zero. So it is having zero. So zero plus one. So its result will be one. Now I hope you understood why I mentioned it here. Because if we don't put zero in this position, then it will be having garbage. And if we add anything in garbage, so that will also become garbage. So before starting this loop, you need to initialize this location. So K is repeated only one time. Now I'm increasing its value. So right now K is one. So see here K I K. So this time it will be zero one. And here it will be one zero. So a zero one is this B one zero B one zero is this. So first row, second element, first column, second element will be multiplied. So both will be multiplied one into one and they will be added to C IJ. So IJ both are still zero zero. So zero zero index will be added and it will become one plus one. Then again K will be implemented. So right now K is two. So this time a zero two and B two zero will be multiplied. So zero two and two zero. So one into one one. And again they will be added to C zero zero index. So this will become three and K you can see three time it is rotated. So it will terminate. Now we will move to J plus plus. Now we will move to J plus plus. So J will become one. J will become one. Come here C IJ. So I is zero. J is one. So C zero one position. So C zero one position at this and it will be zero now. And again cable start from zero again cable start from zero. So this time cable again repeat three times cable again repeat three times and what we need to do. We need to multiply this first row with the second column. So indexes for this first row will be same. So you can also see IK will be same because K will repeat three times. So it will go zero to one one to two. So it will be same but here you can see value of J is now one. So here you need to put these because value of J is one. So this time you can see a zero zero will be multiplied with B zero one. So a zero zero is this and B zero one is this. So both will be multiplied and they will be added to this position. So here also you need to put this zero one. So C zero one will be added with this result right now. Now next index is this a zero one. So a zero one is this and B one one. So B one one is this so both will be multiplied and they will be added to C zero one. So C zero one will become two then multiply these indexes zero two and two one. So a zero two it is this B two one. So B two one is this. So both will be multiplied and three will be available here. So this way again first row of first matrix is multiplied with second column of second matrix and the result is available here. Now K is repeated three times. So J will again in these. So now its value will be two. So here you need to put two. So it means these indexes are same. So again first row of first matrix will be multiplied with third column of first matrix. And the result will be three again that will be stored here. Now K is repeated three times J is also repeated three times. So this time we need to increase I as one and J and K both will start from zero. So it means this time this row will be multiplied with three columns and this these results will be calculated. So I hope this way you understood how these three loops are calculating matrix multiplication and why we have written IK multiplied by BKJ. So try to rotate all these three loops by putting their values side so that you can see at what point of time which variable is having which value so that you can calculate this. Right. So this time you saw all three matrix are having same dimension. So three by three A B is also three by three. So that's why C matrix is also having a three by three result. So this is the case where all the dimensions are same. So now if I change this dimension if I change this dimension. So let's say this is the dimension. A matrix is having a two by three. So let me erase this and create new matrix. So this one is very important. Nothing that I'm going to explain is very important. So let's say two rows and three columns. So this is a matrix and we have the matrix as three by two. So T rows and two columns. So this is our B matrix. Right. So now you need to understand that if you have different rows and columns for both the matrix that you need to first check columns of first matrix and rows of first second matrix should be same. I'm repeating it again. So first matrix is a its column values three. Second matrix is the its low values. If both are same then multiplication can be done otherwise not and the outcome of this will be this two by two. Right. Why so because this first row will be multiplied with this first form. So here you can see three elements will be available and here also three elements will be there. Right. So this first row will be multiplied with this first column and outcome will be this. So this is the matrix. So these three values will be multiplied with these three. Their addition will be stored here. Again this row will be multiplied with this column and their multiplication some will be stored here. Then this row will be multiplied with both the columns. So first it will multiply with this and result will be here. Then again this will multiply with this column and result will be available here. Right. So outcome will be two by two matrix. So if you have different row and column size for both the matrix, then you need to be a third matrix as. Right. And we have two rows. So here we need to change again. So this loop will repeat two times. And this loop will also repeat two times. But this loop will repeat three times because pay is actually multiplying the values. So it will be three times. But for first matrix, we have two rows. So it will be two times for second matrix. We have two columns. So it will also repeat two times. And the outcome will be in the case of two by two matrix. So I hope you understood both the scenarios. If we have same rows and columns for both index, both matrices, then also you can multiply two matrix. And if rows and columns are different, then you need to apply this approach that I explained. So I hope you understood both the scenarios that I explained in this video. If you want to watch more programming related videos, so you can open my channel, go to playlists. So there you will be having a variety of videos. You can also go to detail or description of this video where I have put all the links of playlist related to C language. So you can see them as well. So I hope you enjoyed this video. Thank you for watching this video.