what's happening? why matlab does not save my values? is this a conversion issue?
Yes, it's conversion issue and is not needed. MatLab automatically does conversion for you.
Hence, try storing a
instead of b
imwrite(a, 'test.pgm');
Quoting from documentation of imwrite
imwrite(A,filename)
If A is a grayscale or RGB color image of data type double or single, then imwrite assumes the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values
EDIT
If you want to stick to manual conversion, you need to type cast as uint8
b = uint8(floor(255 * a))