Thank you for the reply sir. I have checked it several times and there is no problem with the matrix shape. I have tried different approaches, but all in vain. I still can not figure out what is wrong with the codes. Please, any other methods for splitting images into small pieces apart from my codes? Any suggestions or recommendations would be highly appreciated.