 Hello, this is Christian. So I want to spend some time in this video to answer one of the questions that was asked during class last night. And the question was how do you build this matrix or build a table using code as opposed to just literally create this matrix by hand, OK? I think that was the question. So I want to do that. And also, I'm going to expand a little bit more on the solutions that we did last night by calculating the diagonals of this matrix to see if they're equal or not equal, OK? And the solution that was proposed by Brent last night is pretty awesome by reversing the matrix and then you do the same loop to iterate, get those columns. So that's awesome. There are many, many ways. But I want to show you how to get the diagonals without using the reversal of the matrix as well, OK? And also some of the shortcuts to build to get those columns. So first, let's build this matrix. And so the first thing you would think about is a matrix is usually, well, I guess, it's a square matrix, right? Usually, it's square. If it's not square, then I guess we just call those a table or two-dimensional tables and so on and so forth. But let's say we all did it with only square matrix. So the rows and columns is always the same. So it's a 4 by 4, 3 by 3, 2 by 12, and whatever. So I'm going to go and create a variable in here called matrix. So let's go up here. And we created matrix variable. You ping that to create it as a default matrix of 0 size. And again, the matrix is basically just a matrix of a two-dimensional of a list of lists, basically. 1, 2, 3. So it's a 3 by 3, and then a 4 by 4, 5 by 5, and so on, OK? So they should always be equal. So we leave that blank first. That's the outer matrix. And then so instead of having, again, we'd like magic numbers. So what is the matrix size? Put here the matrix size. We start with equal to 4. So somehow we have to put 4 somewhere. So that'll be the matrix size of 4 by 4, 5 by 5, and later on, if you want to change the size, you just change in one place, and everything else just works beautifully, right? OK. So the numbers, as you can see in this example, is sequentially from 1 to 16. So 1 to 25, 1 to 32, I mean, 36 and so forth. So it's a square matrix. And you can leave it as is, or you can store a value. You can put something like this. Go up here. Matrix value, or yeah, matrix, let's call it max value, is equal to the square of the matrix size, 4 by 4, 5 by 5, and so forth. So I'm going to do a matrix size star star 2, right? So the star star here is the squared operator. If a single star is a multiplication, so make sure you have it correctly. Again, 4 star star 2 is the square of 4, OK? So it's going to be 16 in this case. So you can do that. You'll use that for some values if you want to do some randomization later on, maybe. I'll just put the info just in case I might need it. So in this case, you can do one way. You can loop a range from 1 up to the matrix size, right? So it's 1, 2, 3, 4, 5. And then you have to break off at the column size of 4. So at the matrix size, when you reach that number, you break it off, and you continue to loop from 5, 6, and 8, and so forth like that. That's one approach. Another simpler approach is basically you start from 1. So that's saying we'll start from element equal to 1. That's my initial number, OK? And then we'll loop here for 4. Every row, we'll start from row 0. For the range of the, you know, doesn't matter, right? For up to the max size, matrix size, OK? So basically row 0, 1, 2, 3, and we're done. We start in that row. And then now we're going to create a temporary list to store the first row data so we can add it to the matrix, OK? So eventually it would be like, let's say 1, 2, 3, 4. And then we'd stop. We had another one, you know, 5, 6, 7, 8, and so forth. That is what we do. It's going to build this temporary list and then add it to the matrix and so forth, OK? So you create a temporary list with that length. And then you're going to do something like four columns. So same thing. We do a I or column doesn't matter for column. And the range of the matrix size, again, same thing. It's a square matrix. And then basically you're going to add to the temp the element. If it's a 1, after that make sure you increase element by 1 to get a 2 and 3 and so forth. So once you do that four times, then you get 1, 2, 3, 4. You're done. Once you're done, you're out of the inner loop and then you're going to add that to the matrix. You're going to append it to the matrix. Sorry, it's kind of noisy. If you can hear, it's almost a vacuum in the room outside. We're going to add the temp to that list, OK? So we do that four times. And then when we're done, we're going to get a matrix of looks like this. So let's see what happens. Let's run this. And here we go. Everything looks good. Run, open the matrix. So here is the matrix for 2, 3, 4. Exactly how you do here, OK? If you want to have something like a non-squared matrix, then all you have to do is basically change this column or this size here. But if you do that, just make sure the numbers are still same. I guess it doesn't really matter. Let's say if I do a, you know, it's a 4, matrix 4. So let's say 4 by 6, OK? So you get a 4 by 6. And here we have a 4 by 6, right? 1 by 6, all the way to 24, and so on, OK? But 1 is square, so I can see I put that square here. So that's how you can actually do that. Now, numbers are sequentially, if you want to use random numbers, then you can import a random function from the random class. Import random. From that random, there is a function called random int that you can use. So let's go down here and let me turn this off. We will do something like this. Random.randint, OK? Has an integer, is integers only. It has a starting and ending position. So these two are integers also, and they will be inclusive, I believe. So I want to start from 1 up to the size of this matrix value. OK, so from 1 to 16, if you change that to something else, maybe after that number. So I do that, and then down here, we'll add the matrix. I will also copy this and do it again. Actually, you don't deduce it. You just do one time down here, actually. I do have to do that. So in other words, you can just put that down here, right here. And then I don't have to do that down here. So I'll randomize that. No, not here, sorry. It has to be in here. You randomize the number. And then you add it to the list. You loop that four times. OK? And so if it's correct, if you run this, you're going to get a set of random numbers. And I did not check for duplicates. So you can see 11 duplicates here. You can also do that. Make sure it is not duplicate. And again, a couple ways to do that, you can do it manually. We can add it to a set. We're going to learn that later. But a set is always unique. And you have to make sure the set size is equal to 4 until you stop, right? So lots of ways. But anyway, so yes, you can randomize your numbers. So let's go back to the original one. And because for sure we know that the numbers actually do work, right? So we want to test something that's actually working first between two something else. So we've got our matrix. And we'll run this again one more time. And I'll leave it here so we can iterate through this. So we're going to get the diagonal from the left to the right. And then we'll do from the left and right to the left and so forth. So I'll do the way we did in the class. And then we'll do it a different way to do that as well. The first thing is, we're going to do a diagonal one and diagonal two equal to two separate lists. So make sure you do that. Separate lists don't set diagonal equal diagonal two because number is reference type. So what do we do here? I'll do the one in the class we did first. So we do a four instead of i, j, I call it row. And the range of the matrix size, it's a 4 by 4. So row 0, how do I get the diagonal? When the i's row is equal to the jth column. So row 0, j0, row 1, j1, and so forth. That's what we did in class. So here you do a four column. And the range of the matrix size again, it's a square matrix. And then we say when they're both equal, so if the row is equal to the column, then we found the diagonal number. And we're going to add that to the diagonal one list. What are we adding? We're adding the data inside the matrix, fb row and jth column. Once you found that, then we don't need to do any further operations. We break out of that inner loop, and then we are done. It goes to the next column, or next row, and so forth. So if it's correct, we run this. Our diagonal should be 1611.16. So 1611.16, that's this diagonal here. So let's do the other one. Let's do the diagonal 2. Now, diagonal 2, if you think about it, so how do you get there without reversing? So you look at row 1, row 0, the index 0. I wish the elsewhere saw index up here as well for the column, but it'll be 0, 0, 0, 1, 0, 2, 0, 3. So I want to do a 0, 3. I get this number. The next one, 1, 0 plus 0 is 1. 1 plus 1 is 2. The column 1 is 2. 1 plus a column 2 is 3. So I get this number 7, that's what I want. So it's a 3. Row 2 plus column 0 is a 9. I don't want that. Row 2 plus column 1 is a 3. It's a 10, so I get that. So you can see that I want to add the i in the j column. If I get to a number 3, I get exactly what I want, this diagonal. So for matrices, the diagonal from this direction is always going to be the size of your matrix, minus 1. You get this diagonal. We add the j in the i or the row, the column together, and they're equal to the maximum index of your matrix that you always get this middle number here, this diagonal. So that's what we're going to do with this here. It's exactly the same as this. I'm going to copy this. It's just one minor change. So this time, for every row, every column, we're going to add the row and the column together. And if they are equal to the maximum matrix size minus 1, in this case, it's 3. If 0 plus 3 is 3, so you get 4. 1 plus 2, you get a 3, so it's 7. 2 plus 1, 3 is 10. So you do that, and you add it to your diagonal 2. And then it's the same. And row 0, column 0, column 3, and so forth. So the change is only basically this line from here to here. So that is our diagonal 2. So if it's correct, we're going to get 4, 7, 10, 13. So we get 4, 7, 10, 13. Cool, huh? Again, just a little bit thinking in math. A little bit just draw it out, sketch it out. You do some calculation in your head and paper until you get it correct, OK? Unless you really, I'm not saying that you're not, but some people are really bright, and they can actually think this in their head and just do it right away. So you can see that's how you do it. Now I'm going to show you another way how to do this, using a much shorter code. It's not a trick. If you think through it, it makes sense. So the first one, you see that with row equal to column, when they're equal. So that means if row is 0, column is 0. Row 1, column 1. So if that's the case, then I don't really care about this column here, right? So let me do this. Let me turn this off, OK? So we'll do a 4 row and range, matrix size. I call it row, but you can call it just a variable. Maybe it doesn't make sense. So I'm just saying that I'm going to go ahead and add this. I don't care about checking whether they're equal or not. I just add it right away. But instead of column, we read the row at the row, right? So row 0, 0, 1, 1, 2, 2, 3, 3. I do that four times, or matrix size times, I'm done. Two lines as opposed to six lines, five lines, right? So that's that. And then same thing down here. Let me turn this off. There's a little math, OK? OK, check first. You can see it's still 1, 6, 11, 16, OK? Now the other way is kind of the same thing. I'm going to copy this. So what's the difference? If you look again, if you go this diagonal, right? It's basically the size of the index minus i. So it's index of 3, maximum index of 3, minus i, you get it 4. 3, minus 1, you get index of 2. 3, minus 2, index of 1, index 0, right? So all you have to do here is basically you can create the column. It's equal to the matrix size minus 2 row, minus 1, OK? That 1 off is the index position, because it's becoming 0. So that's a case that would be from row to column, OK? And I did that three lines, but you can basically move this inside here. You get two lines just like above. I do this so you can see easier. So if it's correct, you've got to get 4, 7, 10, 13, just like the other one. I did not put 2 here. So let's see. OK, so you see that now we have 4, 7, 10, and 13. So again, if you don't like this, you put that inside. If you make it short, you put that inside here. Like that, you delete this, and then here you go, right? OK, so folks, that's how you can actually do this. Again, the other way is as well, OK? This is not the only way. It may not be the most efficient way, but there are other ways as well. Again, this only works in a square matrix. If it's not a square matrix, this may not work, OK? So now the next part is how we're going to check for equal, now I check for equality. So I'm not going to do the summation. You can do that. I'll just use the function built into the Python language. So if the sum of the diagonal 1 is going to add it up for you is equal to the sum of the diagonal 2, then we say yes, otherwise no, right? So again, if you check this, it should say yes, because we did check that they are indeed equal. OK, so here we go. So if you're curious, so once you get that working, you know that it's working, then you can go back and turn on your randomized numbers. So we don't use this. I'll turn that off. So now we're using random numbers, OK? So let's see if it's actually correct or not. So here we go. No, because if you add them up, they're not equal. And we get no, no. So you have to keep trying until you get one. I don't know if it's going to hit one or not, but it might hit. OK, because it's randomized every time. So if you get lucky, you're going to hit one. It says yes. I don't have one. It should work, OK, because we did test it. It actually works. So it's much harder that way. What if you just reduce it by 2? 2, OK, so 2 by 2 is much smaller. So actually, you can see it's better, right? You can see that they're equal indeed. If it's not equal, they're not equal. And then it's not equal. Not equal, not equal. Oh, come on. We have another equal, right? So it does prove that actually our code is such a working. All right, so I'll stop here and also share the code just in case if you're interested in exploring some more. Again, thanks for the questions. If you have any questions further, please do let me know. Thanks, and I'll see you again.