As mentioned above, k-means is already built-in in Matlab as well as fuzzy c-means algorithm. However, the inputs and the output of clustering are not directly an image . You need to pass the image as a vector and after clustering you have to reshape it again to its original dimensions.
You can also have a look on the following line as an example: