Linear Equations -- JHU MATLAB Help Page
LINEAR EQUATIONS
Creating a Matrix
Substituting Within a Symbolic Matrix
Manipulating Regular Matrices
Vectors
Scalar Multiplication
Addition of Matrices
Gauss-Jordan Elimination
Reduced Row-Echelon Form
Dot Product
Rank
Number of Solutions
Manipulating Regular Matrices
How to
Examples
Exercise
How to:
If we had a regular matrix, and we realize that we need to change the number in the i-th row and the j-th column:
A(i, j) = new value
We can even change entire rows or columns all at once. If we wanted to change the entire i-th row of a matrix:
A(i, :) = [New values for i-th row]
(note: the colon ':' is often used in MATLAB to represent "all" of something, so here, A(i, :), MATLAB sees that we are dealing with the i-th row of matrix A, and when it goes to see which column, the colon tells it "all of them")
Now to change the entire j-th column:
A(:, j) = [New; values; for; j-th; column]
Interchanging rows and columns of a matrix is also possible. To swap rows:
A = A([New, order, of, rows], :)
Or to interchange columns:
A = A(:, [New, order, of, columns])
Examples:
Say we had the 3x3 matrix whose values are all 13, R, and we want to change the value in the middle (the 2nd row, 2nd column) to 20:
R(2, 2) = 20
Now, let's change the entire 3rd row to 97, 98, and 99:
R(3, :) = [97 98 99]
And now we want to change the 3rd column to read -1, -2, -3 going down:
R(:, 3) = [-1; -2; -3]
(note: when inputting values, like here, that are in different rows of the same matrix, they must always be separated by a semi-colon)
So, our matrix R currently has 13, 13, -1 in the first row, 13, 20, -2 in the second row, and 97, 98, -3 in the third row. Now, let us swap the top (first) and bottom (third) row:
R = R([3, 2, 1], :)
(note: [3, 2, 1] is the new order of the rows. We are telling MATLAB we want the 3rd row first, the 2nd row second, and then the first row last)
Now R is 97, 98, -3...13, 20, -2...13, 13, -1. Let's swap the 2nd and 3rd columns:
R = R(:, [1, 3, 2])
So, when we're all finished manipulating R, which started off as all 13's, it has 97, -3, and 98 in the first row...13, -2, and 20 in the second row...and 13, -1, and 13 in the third row.
Exercise:
Given a matrix
A = [1 2 3; 11 12 13; 21 22 23]
, in one step, re-arrange it so
A = [11 12 13; 21 22 23; 1 2 3]
.
Click here to see the answer.