MATLAB: Imwrite command changes some pixel automatically

imwrite imread binar image

I run following code
then result is
Why saved image is different than image which I provided?

Best Answer

That has nothing to do with imwrite and everything to do with the image format you've used for saving the image.
Go read the wikipedia article about JPEG compression to learn more about it. In particular the first line of that section says: "JPEG uses a lossy form of compression".
If you care about the exact value of the pixels of your image do not use jpg for storing your images, use one of the non-lossy file format. One of the more versatile is png.