This picture shows a hypothetical phone with an accelerometer.  
The accelerometer is mounted in an ideal way (for us at least), that is, positive Z (blue) pointing 
up trough the screen, positive X (red) pointing towards the right of the screen, and positive Y 
(green) pointing towards the top of the screen.

This setup doesn't need a transformation matrix, resp. the transformation matrix would be an identity 

 X Y Z
 | | |
/1 0 0\
|0 1 0|
\0 0 1/

You can think of the transformation matrix as three unit vectors (X, Y, Z), each representing one axis (X, Y, Z).
(unit vector - vector of size one)

Since our coordinate systems are identical before and after transformation, these are simply `[x,y,z] = [1, 0, 0]` for X, 
`[x,y,z] = [0, 1, 0]` for Y, and `[x,y,z] = [0, 0, 1]` for Z

In this picture, we see an accelerometer that *will* need a transformation matrix. It is mounted 
sideways (or, more likely, it's axes are orineted differently with regards to the package), it is 
either rotated by 45 degrees on the PCB or internally with regards to the package (such especially 
evil positioning is unlikely, but let's go trouhgh with it for better visualization) - and as 
a cherry on the top, the Z axis is inverted compared to what we need (this is definitely not 
a result of evil placement, but simply a manufacturing detail).

So, what shall we do about this?

We need to construct a transformation matrix that can be used to coorectly transform any value 
to the coordinate system that apps expect.

One can construct such matrix by simply constructing unit vectors X, Y, Z (representing axes of our 
desired coordinate system) and describing them using their coordinates in the accelerometer's 
coordinate system.

Let's visualize that with another picture:

picture 3a:

Here we made a unit sphere around the center of the accelerometer's coordinate system 
(you can put this center wherever, we're dealing with vectors), and we used that in conjuction with 
the knowledge of how the accelerometer's coordinate system is positioned with regard to the phone 
(and the screen) to mark the X, Y, Z vectors (their "tips" are represented by red, green and blue 
balls, respectively).

Now all that is left is to note down the coordinates of these points on the unit circle (the coordinates 
of the X, Y, Z vectors) in the accelerometer's coordinate system :)

so, X (red ball): eh, too hard, damn rotation... let's start with Z
therefore, Z: 
              We see that the Z-ball (blue) is positioned on the y axis, so the x and z values will be 0;
              the y value should therefore be 1, because there are *unit* vectors - so here we go:
              `Z[x,y,z] = [0, 1, 0]`
next we do Y: 
              Let's tackle the Y-ball (green) - it's on the unit sphere, and it's also on the XY 
              plane - we can therefore use 2D geometry to figure out it's coordinates \o/

              You can see this idea of transforming our problem into a 2D one on __picture 3a__.
              (though this angle shows a subtle displacement on my part, the arrows are, in fact, 
              supposed to be touching the circle - they have the same origin and are of lenght 1)

              This part-of-a-circle between the positive parts of the x and z axes (on an xz plane) 
              can be written down as `x^2 + z^2 = r^2`. The radius of our unit circle is one 
              (by definition), so `x^2 + z^2 = 1`. We are looking for the middle point (45 degrees, 
              remember?), so we could use the nifty trick of solving `x^2 + z^2 = 1 && x = z`, 
              however let's say we were less lucky: we can use `sin 45°` and `cos 45°`, and we get 
              (from definition) that `x = z = 1/sqrt(2)`.
              so: `Y[x,y,z] = [1/sqrt(2), 0, 1/sqrt(2)]`

here comes X: 
              We can see that the X-ball (red) is on the same plane as the Y-ball, and what's more, 
              it's on the same unit circle! This means our work is almost done - we can clearly see
              that the x coordinate will be the same, while the z coordinate will be inverted 
              compared to the Y-ball. (if your accelerometer is placed in a more ugly fashion, you 
              will likely not be able to use this trick either, but that's out of scope for this 
              `X[x,y,z] = [1/sqrt(2), 0, -1/sqrt(2)]`

And there we have it:
`X[x,y,z] = [1/sqrt(2), 0, -1/sqrt(2)]`
`Y[x,y,z] = [1/sqrt(2), 0, 1/sqrt(2)]`
`Z[x,y,z] = [0, 1, 0]`

Now all that's left is to wite this as a transformation matrix:
(`sqrt()` won't pass, but I'm not sure what the appropriate amount of digits is; a comment noting 
how you got "that ugly number" is probably a good idea though)

 X             Y            Z
 |             |            |
/1,414213562,  1.414213562, 0\

|0,            0,           1|

\-1,414213562, 1.414213562, 0/

And that's all!

used blender file(s): the_blends.tar.xz (phone model from