Linear Algebra with MATLAB

I have the following problem:

write a MATLAB code for computing a matrix of the

orthogonal projection onto a subspace dened as the intersection of column spaces of two matrices A and B.

Note that the intersection of two column spaces is dened only if the

two matrices have the same number of rows. The intersection V1 V2 of two subspaces

V1 and V2 is dened as a set of all vectors that belong simultaneously to both V1 and V2, and it is always a subspace. The challenge is to gure out how to construct a basis of this subspace given the matrices A and B.

The code should be set up as a function with two arguments proj4(A,B). Its output should be a single matrix of appropriate dimensions in case the projection matrix is defined or an error message the projection matrix cannot be computed in case the dimensions of matrices A and B are inappropriate (i.e. the projection matrix is not dened).

This is the code I have so far. However, I'm not sure it is right. Can anybody help?:

function proj4(A,B)

[r1,c1] = size(A);

[r2,c2] = size(B);

if r1 ~= r2

disp('The projection matrix cannot be computed');

else

[R, pivcol] = rref(A);

C = A(:, pivcol);

[P,pivcolb]= rref(B);

D= B(:,pivcolb);

L=[C,D];

Z=null(L);

Z

end

end