First of all, I didn't know where to ask this question, if here or on SO, so I decided to post in both, even if I know we should not do that.

I've an assignment where I basically need to create a function which, given two basis (which I'm representing as a matrix of vectors), it should return the change of basis matrix from one basis to the other.

So far this is the function I came up with, based on the algorithm that I will explain next:

function C = cob(A, B)
% Returns C, which is the change of basis matrix from A to B,
% that is, given basis A and B, we represent B in terms of A.
% Assumes that A and B are square matrices

n = size(A, 1);

% Creates a square matrix full of zeros 
% of the same size as the number of rows of A.
C = zeros(n);

for i=1:n
    C(i, :) = (A\B(:, i))';


And here are my tests:

clear out

S = eye(3);
B = [1 0 0; 0 1 0; 2 1 1];
D = B;

disp(cob(S, B));  %  Returns cob matrix from S to B.
disp(cob(B, D));
disp(cob(S, D));

Here's the algorithm that I used based on some notes. Basically, if I have two basis $$B = \{b_1, ... , b_n \}$$ and $$D = \{d_1, ... , d_n \}$$ for a certain vector space, and I want to represent basis $D$ in terms of basis $B$, I need to find a change of basis matrix $S$. The vectors of these bases are related in the following form (according to some notes that I've):

$$ \begin{pmatrix} d_1 \\ \vdots \\ d_n \end{pmatrix} = S * \begin{pmatrix} b_1 \\ \vdots \\ b_n \end{pmatrix}$$

Or, by splitting up all the rows:

$$d_1 = s_{11} * b_1 + s_{12} * b_2 + ... + s_{1n} * b_n$$ $$d_2 = s_{21} * b_1 + s_{22} * b_2 + ... + s_{2n} * b_n$$ $$...$$ $$d_3 = s_{n1} * b_1 + s_{n2} * b_2 + ... + s_{nn} * b_n$$

Note that $d_1$, $b_1$, $d_2$, $b_2$, etc, should all be column vectors. This can be further represented as

$$d_1 = \begin{pmatrix} b_1 & b_2 & \cdots & b_n \end{pmatrix} * \begin{pmatrix} s_{11} \\ s_{12} \\ \vdots \\ s_{1n} \end{pmatrix}$$

$$d_2 = \begin{pmatrix} b_1 & b_2 & \cdots & b_n \end{pmatrix} * \begin{pmatrix} s_{21} \\ s_{22} \\ \vdots \\ s_{2n} \end{pmatrix}$$


$$d_2 = \begin{pmatrix} b_1 & b_2 & \cdots & b_n \end{pmatrix} * \begin{pmatrix} s_{n1} \\ s_{n2} \\ \vdots \\ s_{nn} \end{pmatrix}$$

Lets call the matrix $$\begin{pmatrix} b_1 & b_2 & \cdots & b_n \end{pmatrix}$$ whose columns are the columns vectors of $B$, $A$.

Note that what we need now to find are all the entries $s_{ij}$ for $i=1...n$ and $j=1...n$. We can do that by left-multiplying both sides by the inverse of $A$, i.e. by $A^{-1}$.

So, $S$ might look something like this

$$S = \begin{pmatrix} s_{11} & s_{12} & \cdots & s_{1n} \\ s_{21} & s_{22} & \cdots & s_{2n} \\ \cdots \\ s_{n1} & s_{n2} & \cdots & s_{nn}\end{pmatrix} $$

If this idea is correct, to find the change of basis matrix $S$ from $B$ to $D$ is really what I'm doing in the code.

Is my idea correct? If not, what's wrong? If yes, can I improve it?


Basically, your explanation is hard to follow because of a mixture between column and row vectors.

You should stick to a certain convention: every time you consider a vector known through its components with respect to a a basi, connect it with a column vector (this is the most usual convention), with notation $V$, and stick to notation $V^T$ if it is a row vector.

If such a convention is used, if we consider $B = (b_1, ... , b_n)$ and $D = (d_1, ... , d_n)$ (where the $b_k$ and the $d_k$ are column vectors), then, $B$ and $D$ are $n \times n$ matrices. We have the equivalence:

$$(\forall k, Sb_k=d_k) \ \Rightarrow \ SB=D \ \ (1)$$

From formula (1), the matrix you are looking for (as you say more or less) is simply

$$S=DB^{-1} \ \ (2)$$

For example, if you want the matrix $S$ sending $b_1=$$1 \choose 1$ and $b_2=$$-1 \choose 1$ to $d_1=$$0 \choose 1$ and $d_2=$$ -1 \choose 0$ resp., formula (2) gives:

$$S=\begin{pmatrix} 0 & -1\\ 1 & 0 \end{pmatrix} \begin{pmatrix} 1 & -1\\ 1 & 1 \end{pmatrix}^{-1}=\frac{1}{2}\begin{pmatrix} 1 & -1\\ 1 & 1 \end{pmatrix}$$

  • $\begingroup$ Regarding the confusion about what's a row and what's a column vector, I also was confused reading the nodes that I have. I just tried to report what I have, with the hope that maybe you would understand better than me those notes. $\endgroup$ – nbro Feb 27 '16 at 21:54
  • $\begingroup$ You have not to be confused. It has taken me also a certain time to have clearcut methods in mathematics in general and in linear algebra in particular. $\endgroup$ – Jean Marie Feb 27 '16 at 21:57
  • $\begingroup$ Regarding your solution, I'd watched a few video lectures from Khan Academy, and Sal shown a solution similar to yours. The problem is that in the notes given by my professors (or TAs), their explanations are basically the explanations I'm writing above. In the question I asked here, you can find a similar explanation given by another professor I had some time ago. $\endgroup$ – nbro Feb 27 '16 at 21:59
  • $\begingroup$ By the way, just a curiosity (or a guess), you don't live (or ever lived) in Lausanne, Switzerland, right? $\endgroup$ – nbro Feb 27 '16 at 22:08
  • $\begingroup$ No, I have been at EPFL several times, but never lived there. $\endgroup$ – Jean Marie Feb 27 '16 at 22:18

There is a third basis implicitly in the picture: the basis that the basis vectors themselves are expressed in. The "standard" basis.

Multiplying a vector by $B$ forms a linear combination of the $b$ basis vectors, with coefficients given by the input vector entries. The output vector is expressed in the standard basis, since it is a linear combination of vectors $b$ that are expressed in the standard basis. Hence multiplication by $B$ converts vectors from the $b$ basis to the standard basis.

And of course multiplication by $B^{-1}$ converts the other way, from the standard basis to the $b$ basis, since the composition of a matrix and it's inverse must get you back where you started.

Likewise multiplication by $D$ converts from the $d$ basis to the standard basis, and multiplication by $D^{-1}$ converts from the standard basis to the $d$ basis.

Hence to convert a vector from the $b$ basis to the $d$ basis you can first convert from the $b$ basis to the standard basis by multiplying by $B$, then from the standard basis to the $d$ basis by multiplying by $D^{-1}$. Altogether, multiplying by $D^{-1}B$.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.