There are two different ways that are commonly used to represent quaternions.
Q = [a,b,c,d] with d being the scalar part, and a,b,c being the vector part.
Q = [a,b,c,d] where a is the scalar part and b,c,d represent the vector part.
Matlab currently uses the latter representation; a is the scalar, and b,c,d are the vector coponents. Make sure that your code is the same.