Working with a matrix + nested loops
Published: 2020-12-26 21:56:47

Hello! Happy holidays to whomever might be reading this =). Today I am going to write about nested loops. I have been coding for about a year now, and I have dealt with nested loops often. However, I still find them a bit difficult sometimes to picture what I am doing. Considering how often I think they are used though, with matrices and whatnot I think that it could be valuable to go over how I think about them and visualize moving through a matrix. 

So to start out, I think that it is important to go over what we are working with when using nested loops. Let me clarify something before anyone gets confused. Nested loops can be used for a lot of different things. However in this blog post I will be examining nested loops as pertaining to a matrix, ie, an array of arrays. I think that thinking about a matrix can be useful in visualizing how a nested loop can move through things. 

So a common problem one might encounter that would possibly call for a nested loop would be transposing a matrix. In this particular example we are flipping the row and column index of a matrix over its diagonal. Something like this. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w56ciw9vha98aqisq06j.jpg)

The first time I used matrices was when I took linear algebra. To be honest I actually liked the class. It was pretty fun in an academic way. But to this day I am not entirely sure why using matrices is that important. Now it has definitely been a while since I took linear, and I am sure the professor said something as to a practical application but for now I assume that it can be useful in calculations that might be above my head. 

Lets talk about how we use a nested loop to transpose a matrix. Imagine a matrix written out as an array of arrays. We might have something like [[1,2,3],[4,5,6],[7,8,9]]. Sometimes it makes it a bit easier to me to visualize by making it look more matrix-like : 
[
[1,2,3],
[4,5,6],
[7,8,9]
]
What we want to do is be able to make a nxn matrix transpose over the diagonal so in this case we would want : 
[
[1,4,7],
[2,5,8],
[3,6,9]
]
The way we go about this would be using a nested loop. The first thing I did was to make a result array. It is possible to change the original matrix, but I thought it might be easier/better to just make a new one. Then we make our first loop. Our first loop will start with the first element in our array which is the first array within the array ([1,2,3]) this will deal with our *rows*. The loop will go through each element within this array. Before we make the inner loop we need an array holder to make our inner arrays. Then we make our inner loop which will go through the length of the whole array, our *columns*. 

As we run through the loop we start with the first element of our row and col. Which in this case is 1, and this gets pushed into the inner placeholder array. So far we have our outer array and one inner array which at the moment contains just 1. Then we move to the first element of the second row and push that into our placeholder array. So now our placeholder array has 1, 4. Finally we move to the first element of (in this case) our last row. That gets pushed in and now we have our first new row completed : [1,4,7]. 

This will continue now with the next element which would be like going down the next column. Eventually using this method any size matrix would be transposed. If any of that didnt make sense maybe this picture will help. 

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uklz0fco0z8obqa4r4r0.jpeg)