This rotation can be easily accomplished with what are known as direction cosine matrices. These matrices are single-axis rotations, which when multiplied together, accomplish an entire rotation from one orientation to another. You often encounter these topics in an attitude dynamics course.
Note, it is very important that you know which angle you want to rotate through first. Rotations done in a separate order will often not yield the same results. Here are the steps to accomplish your rotation.
1.) Define DCM's (direction cosine matrices). Note, these are 3x3 matrices.
The DCM about the X axis through the angle A. Call this matrix M1.
[1 0 0
0 cos(A) sin(A)
0 -sin(A) cos(A)]
The DCM about the Z axis through the angle B is: (Call this matrix M3)
[cos(B) sin(B) 0
-sin(B) cos(B) 0
0 0 1]
2. Combine the DCM's to yield a rotation matrix from v1 to v2 (your new vector). To combine DCM's, we place the first DCM on the right, and every subsequent rotation on the left: Let the entire rotation matrix be called [V2V1] to read, "to V2 from V1":
[V2V1] = [M3(B)][M1(A]].
3. With this full rotation matrix, we simply multiply V1 by it to get V2, i.e:
V2 = [V2V1][V1]
Expanded: V2 =
[-sin(B)sin(A)
-cos(B)sin(A)
-cos(A)]
4. If you want to rotate through B first, and then A, V2V1 = [M1(A)][M3(B)] instead.
So if you just want to code the rotation generically, use the Expanded V2 shown above, it's V1 rotated first by an angle of A about the X axis, and an angle of B about the Z axis.
For cleaner formatted derivation, see:
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B8omXOGmi-DdNzZlYTUwNDQtNzBkMy00M2U0LTlmOTYtZDAxNzBiZWYzZTEw&hl=en_US