**3.9 Creating a Matrix from a Matrix**

Now that we have covered graph plotting, let’s look how to create a matrix from a matrix before we apply all of what we have learned to the t_z matrix from **Example 2.2**.

You might be wondering why on Earth one would want to further complicate his/her life by creating a matrix from another one. You’d be surprised to find out that it makes life a lot simpler.

**3.9.1 Introducing the concept of creating a matrix from a matrix**

Let’s first consider what creating a matrix from a matrix actually means. Let’s say you had the coordinates of ten capitals saved in a 10×2 matrix called capital_coordinates. The coordinates are saved such that x-coordinates of the capitals lie in the first column of the matrix and such that the y-coordinates lie in the second column. What if we wanted to create a plot that showed the x-positions of the capitals relative to one another without considering the y-coordinates?

We can do this by considering how matrix indexing works (refer to the **matrix indexing** section of our MATLAB tutorial series). Applying concept of matrix indexing to extract individual columns from an mxn matrix, you can plot the first column of the capital_coordinates to extract the x-coordinates of each capital. To extract the first column of the matrix, you can use the command: `capital_coordinates(:,1)`

. Now, to plot all the x-coordinates to a 1D set of points, you’d need to use the command: `plot(capital_coordinates(:,1),0,’x’)`

. Download and run the M-file within the attached zip file to understand what is going on.

Download:

- Required file: coordinates_of_capitals.zip

Note: Why did the command include a 0 and an ‘x’? Well, we want a 1 dimensional plot so we want all the y-values to be 0. And what about the ‘x’? Well, we would like MATLAB to show the x-coordinates as different coloured crosses (refer back to the section on marker types).

What if we wanted to have a bird’s eye view of the locations of the cities relative to one another? We could plot the x-coordinates of each capital to their y-coordinates using the same concept. The command for that would be: `plot(capital_coordinates(:,1),capital_coordinates(:,2),’x’)`

.

I’m sure you’d agree that this is quite a long command and that there must be a better way to do this, and there is! This is where creating a matrix from another matrix comes into play. We can massively simplify the plot command and also the indexing by creating a new variable that directly relates to the x-coordinate column and another one to the y-coordinate column of the main matrix.

We can do this using the following code:

```
x_capital = capital_coordinates(:,1);
y_capital = capital_coordinates(:,2);
plot(x_capital,y_capital,'x');
```

Much better on the eyes, isn’t it? Download and run the M-file in the attached zip file to see this in action.

Download:

- Required file: coordinates_of_capitals_simplified.zip

**Exercise 3.3**

Can you add labels to each of the coordinates to identify the coordinates? The list of cities and their coordinates are given in the names_of_capitals.txt file within the attached zip file.

Download:

- Required file: names_of_capitals.zip

Hint: you can do that by using the TextBox tool in the figure window in the ‘Insert’ menu. You should end up with a graph just like **Figure 3.9**.

**3.9.2 Applying the concept of creating a matrix from a matrix**

Here, we will apply the concept introduced to the t_z matrix from **Exercise 2.2**. For easier reference, the t_z data is again shown below.

Displacement at certain times t(s) z (m) 0.0 1.000 0.1 0.955 0.2 0.900 … … … … 10.0 0.270

Let’s say we wanted to plot z against t. We would first import it into a variable (say called `t_z`

) and we can then do of two things to create the plot. We can either

- use the indexing
`t_z(:,1)`

and`t_z(:,2)`

in the plot function to represent t and z respectively, or - create new t and z variables using the indexing and then use the new variables in the plot function.

For the first option, the code would be:

plot(t_z(:,1), t_z(:,2));

For the second option, a possible code would be:

t = t_z(:,1); z = t_z(:,2); plot(t,z);

Which one you choose is up to you. The second option of creating two new variables includes more lines but simplifies massively the plot function.