 # Plotting arrays in MATLAB

how to plot a matrix in matlab This is a topic that many people are looking for. g4site.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, g4site.com would like to introduce to you Plotting arrays in MATLAB. Following along are instructions in the video below:
This video. Im going to give you a quick overview of the simplest plotting plotting commands in matlab. Okay.
The general idea to plot in matlab is to define of x and y coordinates. So were going to have one array containing the x coordinates of some point ruben plots and another array containing the y coordinates of the corresponding port right. And then were going to plot the values of y against those of x ok.
So. Thats the general idea of how it works. So how do we do that well there are several ways so let me introduce a simplest one where we only define one array.
So let me define. One array called y1 and here. Im going to use the definition of the array using square brackets ok so between square bracket.
I can just plug in some values separated with a space. And that way the array is going to contain these values. So i have 3 0.
Negative. 2 1. 3.
5 7. 5. And 2 okay.
Ive taken some completely arbitrary values here okay so i define an array. Which should contain 10 elements ok and to show you that it actually works if i now print the array of a1. Yes.
I do get these values right good. So now what happens. If i just plot y1 ok.
So here here. Ive only defined the y coordinates of the points. I want to plot ok not the x coordinate.
So if i just write plot of y1 well. What my flap is going to do is to plot these values against the first 10 integers okay so here because it has 10 values ok. The size of the array is 10 then it plots these values against all the integers from 1 to 10.
Okay if it has 15 entries in the array. Then it will plot the values against all the integers from 1 to 15 are key anyway. So thats how it works right here matlab.
Just assigns the x coordinate of the points to be the first 10 integers because we havent specified them ok now were gonna see what happens when we actually specify the x coordinates as well just another thing you can add options to products okay. So for example instead of protein of plot y1. I have plot y.
1. And then in apostrophes like that i can put an options for example of color. So if i put all my protease in red.
Okay and g correspond to green as well and i also can plot with markers so for example if i want to stop. But thats ok then there you see my plot my data i look like stars right and i can also combine them so for example. If i want to plot my data with green pluses.
So i would have g plus like that and matlab understands that and you see now i have my green process. Okay alright so those are just options that you can add okay to your plots to make it you do what you want now. See what happens.
When we actually define the x. Coordinates as well so now let me define my an array x1. Which is going to contain the x coordinates of the points.

That i have defined here right so of course. The array of x coordinates must have the same number of elements. As the array of y coordinates.
Okay so well this time. Im going to plot these values against the integers from 5 to 14 ok. Its also 10 integers.
I will have 5 14. So its the array containing all the integers between 5. And 14 right so it should be 10 of them just to convince you if you print x1.
You actually get five six seven eight etc. Until 14. So now how do i put the array of y coordinates y1 against the array of x coordinate x1.
I simply use a plot command again so plot and in brackets first the array of x coordinate. It is important to be in that order. And then the array of y coordinates okay so if i plot that you see an error.
I have of course the same y coordinates. But you see they are plotted against all. The integers from 5.
To. 14 ok. 5.
6. 7. 8.
9. Etcetera. Alright.
And of course. You can also add some options the same way that weve done before okay so for example. If i want some red crosses.
Okay. I would have all x. Ok.
X. Represent. A cross and thats what i have here.
Okay. So i have red cross this for my data good. Now.
Lets see one more thing is how do we plot two sets of arrays or two sets of data points on one figure. Okay well. Its not very difficult all we need to do is define two more arrays of x and y coordinates and were going to use the same plot command.
As we see so first let me define another array of x coordinates. For example x2 and im going to make it equal to the array containing the integers from 3 to 12 so 3 to 12 right do you see that x2 to contain all the integers from 3 to 12. They are 10 of them and let me define a second array of work coordinate y2 and im going to take the value 0 1.
0. Negative to the for negative one. One two five and three okay.
I should have also ten values in here here. And if i print y to they have the that the entries here of the array. So now i want to plot.

Y. 1. Against x 1.
And y 2. Against x 2. In the same figure.
Okay so im going to use the same plot. Command. And then im first going to put y 1.
Against x 1. So first remember the x coordinate come on then the y coordinates okay and then some options so for example. I want to plot that one in in red.
Okay and then after the options. I can add more arrays okay and thats what im going to do here. So.
Now. Im going to provide my values containing y. 2.
Again. Those in x 2. So.
First. Two x. Coordinates.
Then the y coordinate and then the options so for example. Im gonna brought that one to be green lets say and of course. I need to close the bracket okay.
But lana took are not limited to only two sets of race. Here. I can continue.
Okay. With. X.
3. Y. 3.
X. 4. Y.
4. And stop. Wherever.
I want okay. I can you can plot as many sets of arrays in one figure as you want using this broad comment. So lets see what we have now and thats what we have so you see in red that was my first set of points ok.
Which are still plotted against 5. To 14 right so we start at 5 and at 14 in green now i have my sets of points ok. Containing y2 plotted against the integers from 3 to 12.
So we see our 3 and that okay so thats way it works for protein. Two arrays. Okay.
There are many more command and way of plotting matlab here as just a simplest one okay. But they are very helpful for what were going to see next .