projection of a onto v in matlab

Assuming that a and v are column vectors in MATLAB, we need to compute the projection of a onto v. The projection of a onto v can be computed as:

main.m
projection = ((a'*v)/(v'*v)) * v;
34 chars
2 lines

Here, (a'*v)/(v'*v) computes the scalar projection of a onto v and (v'*v) computes the magnitude of v. Finally, we multiply the scalar projection with v to get the projection of a onto v.

The full MATLAB code would be as follows:

main.m
a = [1; 2; 3]; % column vector
v = [4; 5; 6]; % column vector
projection = ((a'*v)/(v'*v)) * v;
96 chars
4 lines

The variable projection will now contain the projection of a onto v.

related categories

gistlibby LogSnag