How to conveniently do cross product of a 3x3 matrix with a 3d vector in matlab?

Qiang Li picture Qiang Li · Dec 7, 2010 · Viewed 7.4k times · Source

for example,

magic(3) x [1,2,3] gives:

-9   -18    15
 1    -2     1
23   -10    -1

Answer

gnovice picture gnovice · Dec 7, 2010

It sounds like what you want to do is compute the cross product of each row of a 3-by-3 matrix with a 1-by-3 vector. In order to use the function CROSS, the two inputs must be the same size, so you will have to replicate your 1-by-3 vector using the function REPMAT so that it has three rows. Then perform the cross product along the columns:

>> A = magic(3);
>> B = [1 2 3];
>> C = cross(A,repmat(B,size(A,1),1),2);
C =

    -9   -18    15
     1    -2     1
    23   -10    -1