Skip to main content

Section 9.3 Creating Matrices

Recall that a matrix has numbers in both rows and columns. They are used extensively in science and engineering and are useful for storing numbers in table-like formats or for other linear algebra techniques that you will learn in future classes.
The terminology we will use in this class is to describe matrices by the number of rows and columns they have. Furthermore, you can have either square or non-square matrices depending on the number of rows and columns. Consider the following two matrices:
Figure 9.3.1. Examples matricies.
In figure 9.3.1 above, we can see that the matrix square has 3 rows and 3 columns. To make it easy to write, it is common to say that this is a 3x3 (pronounced β€œthree by three”) matrix. Square matrices have the same number of rows and columns. It follows that since not_square has 4 rows and 3 columns it is not square. We would say this is a 4x3 matrix (pronounced β€œfour by three”).
Creating matrices in MATLAB is as easy as combining what we have learned about creating row and column matrices above. To create a matrix you type:
matrix_name = [1st row elements; 2nd row elements; ... ; Last row elements]
Try It!
Let’s try making our own matrix by typing the following into the command window:

Example 9.3.2. Code Your Own Matrix.

Here is an interactive code box that functions identically to the MATLAB command window.
Notice that this creates a 3x3 matrix called neo. The only difference between matrices and vectors is that all the rows must have the exact same number of elements. For example, you can’t have row 2 have 3 elements, while row 3 has 4 elements.

Checkpoint 9.3.3. Matrix Dimension Error.

What error is displayed if you try to create a matrix in MATLAB where the number of elements in each row is not the same?
  • The dimensions requested are not available.
  • Incorrect. This is not a standard MATLAB error message for matrix construction.
  • The number of elements in each row is not equal.
  • Incorrect. This is a description of the problem, not the actual MATLAB error message.
  • Dimensions of arrays being concatenated are not consistent.
  • Correct. MATLAB returns this error when rows have different lengths and cannot be combined into a valid matrix.
  • Should not have been checked.
  • Incorrect. This is not a MATLAB error message.
Creating Arrays of all 0’s or all 1’s
Sometimes it is useful to create vectors or matrices with all zeros or all ones in their spots. You could create a 4x4 matrix with all ones by typing this:
tedious_ones = [1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1]
You can imagine that creating a 4x4 matrix with all 0’s would be equally as annoying. But as we have learned, if something sounds tedious, there is a better way to do it. MATLAB has built-in functions that will do this automatically for us.
To create an array of all 1’s you use the MATLAB function ones() as follows:
all_ones = ones(rows,columns)
Similarly, to create an array of all 0’s you use the MATLAB function (you guessed it) zeros() as follows:
all_zeros = zeros(rows,columns)
In both cases, you only specify the dimensions of the output array. For example, if you want a variable named skinny_boi to be a 5-element column vector with all ones you would type in:
skinny_boi = ones(5,1)
As always, do your brain workout. Play around with both the ones and zeros commands to make sure that you can create row vectors, column vectors, and matrices using the commands. As usual, store them in different variable names and notice how they appear in the workspace.

Checkpoint 9.3.4. Identifying a Row Vector of Ones.

Given the following MATLAB commands:
>> thomas = ones(9,1) >> BANANA = ones(1,9) >> rhino = ones(9,9)
Which variable will be a row vector of all ones?
  • rhino
  • Incorrect. This creates a 9Γ—9 matrix of ones, not a row vector.
  • BANANA
  • Correct. ones(1,9) creates a 1Γ—9 row vector of ones.
  • thomas
  • Incorrect. ones(9,1) creates a column vector of ones, not a row vector.
The Transpose Operator '
Remember, that if I want a row vector variable called rex containing the elements 2,4,6,8,10 I can simply type in rex = [2:2:10]. But what happens if I want a column vector that contains those elements? Tedious alert! That means there is a shortcut. In this case, we are talking about the transpose operator ’.
The transpose operator will switch the row to columns of any type of an array. That means if you apply the transpose operator to a column vector, it will turn into a row vector. If you apply the transpose operator to a matrix, it will switch the rows with the columns.
If you ran the rex = [2:2:10] line of code above in the MATLAB command window you can create a new column vector named tex by typing in tex = rex'.
Given the image of the user’s workspace, consider the command trinity = neo'.

Checkpoint 9.3.5. Understanding Matrix Layout in MATLAB.

What would the variable trinity look like in the workspace after typing the following command?
trinity=neo'
  • trinity = ​[7,8,9;4,5,6;1,2,3]
  • Incorrect. This is not what the command produces.
  • trinity = [1,4,7;2,5,8;3,6,9]
  • Correct. This rearranges the matrix into columns as rows (a transpose-like pattern).
  • trinity = [9,8,7;6,5,4;3,2,1]
  • Incorrect. This reverses each row, which is not what the command specifies.
  • trinity = [1,2,3;4,5,6;7,8,9]
  • Incorrect. This is a reordered version of the matrix, not the one produced by the command.
You have attempted of activities on this page.