# Create Vectors and Matrices

**A free video tutorial from**Tim Buchalka's Learn Programming Academy

Professional Programmers and Teachers - 1M+ students

4.5 instructor rating •
52 courses •
1,076,736 students

## Lecture description

Create column and row vectors, and matrices.

### Learn more from the full course

Master MATLAB through Guided Problem SolvingBecome an expert in MATLAB Programming and Scientific Computing. Advance your career in Engineering Physics Biology etc

37:55:43 of on-demand video • Updated January 2021

- Intermediate to advanced MATLAB programming proficiency. This is *the only course* that develops intermediate and advanced programming skills.
- Obtain real-world application experience that that researchers and industry professionals use MATLAB for, including signal processing, matrix decompositions, spectral analysis, linear and nonlinear model-fitting, and statistics.
- Gain skills to solve challenging problems in MATLAB, as opposed to memorizing syntax rules.
- Data science projects and problem-solving in MATLAB

English [Auto]
In this video you're going to make vectors and matrices and matlab principally using square brackets and the semi-colon. The bonus is to use the functions ones zeros and Rande and to make these vectors automatically using these functions. This will practice your skills using these functions and concatenation using square brackets as well as the transpose operator to turn a row vector into a column vector and vice versa. Let me first show you what the difference is between column vector row vector and a matrix. Actually if you just interpret the words it's pretty obvious. A column vector means that there is one element to one number per row and then there's a bunch of columns a row vector means that there is just one row of numbers and a matrix you can think of it like a bunch of row vectors that are stacked on top of each other or a bunch of column vectors that are stacked next to each other. Basically a matrix is just made a table of numbers. So let's switch to Matlab and see how we can make some of these things we'll call these factors variable vector. That's a pretty good variable name. It helps you understand what that variable contains a vector. So the instruction is here in the comments says make a row vector using square brackets. So fair enough so do open brackets and let's just say 1 2 3 4 2 3 1 0. Maybe I'll throw in a negative number in there just for good measure. So now we've successfully created a row vector. How do I know that it's a row vector. Well one way is I can just evaluate this code and I can see that fact the output is a row vector like this. I can also type whose effect. And that will tell me that this is a one by nine vector. So it's one element deep in this dimension and nine elements over in this dimension. Now the next one says make a column vector using semi-colons. So to make a column vector using semi-colons what you want to do is put a semi-colon in between each of the numbers just like this. You don't need to do it for the last one. Now this can be a little bit confusing at first because the semi-colon inside these square brackets indicates that we should skip to the next line. So two and then four is going to go into the two or six We'll go under that and so forth. And the semi-colon at the end of the line outside the square brackets indicates that Matlab should suppress the output of that line. So let's see what fact is then I will type just fact in the command window and you can see that that forms a column vector. Actually maybe I'll call this fact are for row C for column now I can type who's And I can see that Vecsey is a column vector and vector are is a row vector. Next I want to make a column vector so similar to this except now I'm going to make this column vector by transposing a row vector. So I'll start again one two three four whatever. Just some random numbers in here. Now this we've already established is a row vector but I can transpose it to generate a column vector transposing it means you convert all the rows in the columns and all the columns into rows and in Matlab you can indicate the transpose using the single apostrophe. So now I can run this. Now I'm not running the apostrophe. Yes. Now this is a row vector and if I run this again including the apostrophe then you'll see it becomes a column vector. So the apostrophe converts the row vector into a column vector. You can also use the function transpose. So that would look like this function transpose. And then inside here. As the input argument into this transpose function is a row vector and you'll see that that will also produce a Culham vector because the Rove actor gets transposed next. We want to create a two by three Matrix. So this is a matrix that has two rows and three columns. And the way that you create a matrix in Matlab is by combining what you learned here about romancers Rove actors with what you learned here about making Kulim backers using the semi-colon inside the square brackets. So what I'm going to do is specify the first row and now I use a semi-colon and then I'll specify the second row like this. I can run this code and you can see it's a two by three Matrix. This is the first row and this is the second row. So I would like you to guess what's going to happen when I run this line of code now so we get an error and it says the dimensions of matrices being made that are not consistent. See if you can figure out why we get this air and how to fix it. So the reason why we get this error is that in Matlab and also in linear algebra in general for a matrix all of the rows have to have the same number of columns and likewise all the columns have to have the same number of rows. And that's not the case here. So this first row is three elements. The second row has two elements so that's not allowed. So there's two ways to fix this. Either you can add another element here or you can take away another element here then this becomes a two by two Matrix. Now I mentioned before that transposing a vector will turn the rows and columns and the columns into rows. Same story for a matrix. If you want to make a two by three Matrix. You can also start with a three by two matrix and then transpose it. So let's see that now. So I'll make a three by two Matrix. So this is the first row. This is the second row. And this is the third row. And we can confirm that this is a three by two Matrix. You always start counting the rows and then over the column. So it's three by two. And now I can transpose this matrix. And that gives me a two by three Matrix. And you'll notice that the first row the first column became the first row and the first row became the first column. Now I want to make a row vector of all ones. Let's call this one stack so you can do something like this. Just put lots of ones inside these brackets. That's OK if you just have a couple of ones. But let's say you want to generate one thousand one so a row vector with 1000 elements in every element is the number one. So you don't want to sit here all night and do this that's going to take forever and you'll probably lose count. So instead a better solution is to use the function called once. And this is one of the bonus exercises. So the way that you use the ones function is you type ones and then the input is going to be the number of ones that you want to generate. So let's say four. So it seems like this should be the right thing to do it seems like this should produce a vector of all ones. But in fact it won't. This will actually produce a matrix a four by four matrix of all ones. And that's the default behavior for Matlab when you give this function ones and similar functions like zeros and round and several other of these functions. Now the default behavior is to produce an end by end matrix for a single input. So instead what you want to do is give two inputs and if you want to factor then the other input should be 1. Now based on what I wrote here without running this line of code figure out if this is going to produce a column vector or a row vector. So in fact this produces a Culham vector because they specify that the geometry should be four down and one across. So if you want a row vector of ones you will have to write one comma for. And we can confirm that this is the right solution. OK here's another bonus problem. Generate a column vector where every value in the column contains the number. Point to five. So there are several ways to solve something like this. First of all you can already guess that something like this is technically correct. This will work. But this is not a great solution for the same reason that this is not a great solution. One thing you can do is use the function once again. And now we do want a column vector. So let's say it's going to be 8 elements long. Now this is not exactly correct yet because I'm generating all ones but of course I can divide this by four or multiply by 25 and that will give us the result that we want. Another way to solve this problem is to use a complimentary function called zeros and the zeros function works. So I think that these zeros zeros function works the same as ones except it generates zeros. And now obviously we have to say plus point to 5 instead of times 2.5. Finally we want to generate a matrix of random numbers and to do this we can use the function rand and which will generate normally distributed numbers. That means that the average of a lot of these numbers is going to be zero and the standard deviation will be 1. There's actually many functions in matlab that will generate random numbers with various distributions and you'll learn more about these other functions in the later videos. Another common one for example is rende which will generate uniformly distributed numbers that vary between 0 and 1. But here I'll use round and let's say I want a four by three Matrix. So this is a bunch of random numbers and you can run this line again and you get different numbers because these are being randomly generated each time we call this function.