The intended result is to transform the coordinate such that the range [Near,Far] maps to [0,1], but after the perspective divide.
Ignoring the divide to start with, we start by translating by -Near, so that Near maps to 0.
Zout = Zin - Near
Now, in the given case, Z values at the near plane become 0, Z values at the far plane become 9. We rescale by 1/(Far-Near) to bring that to the range [0,1]
Zout = (Zin - Near) / (Far - Near)
To make this easier to calculate with a matrix, we want it in the form A * z + D, so we distribute and rearrange things
Zout = Zin * 1/(Far - Near) - Near / (Far - Near)
If it is an orthographic projection, we're done. If it is a perspective projection, we must take into account the divide by Zin that will happen.
Zclip = Zin * 1/(Far - Near) - Near / (Far - Near)
Zout = Zclip / Zin
For Zin = Near, Zclip is 0.0, and nothing would change, but for Zin = Far, we would get a result of:
Zclip = Zfar * 1/(Far - Near) - Near / (Far - Near) = 10 / 9 - 1 / 9 = 9 / 9 = 1
Zout = Zclip / Zin = Zclip / Far = 1 / 10
To get a Zout of 1, we have to scale things by Far, which will give the correct result of Zin = Near -> 0.0, Zin = Far -> 1.0. Distributing it across:
Zclip = Zin * Far / (Far - Near) - (Near * Far) / (Far - Near)
Zout = Zclip / Zin