projection of w onto b in matlab

To project vector w onto vector b, we may use the following formula:

proj_b(w) = (dot(w,b)/dot(b,b)) * b,

where dot denotes the dot product of two vectors.

In MATLAB code, this can be done as follows:

main.m
w = [1 2 3];
b = [4 5 6];

proj_b_w = (dot(w,b)/dot(b,b)) * b;
63 chars
5 lines

Here, w and b are the input vectors, and proj_b_w is the projected vector of w onto b.

related categories

gistlibby LogSnag