Hough Transform in Matlab without the built-in function
Check this out. It is an easy to follow implementation. You can compare your implementation against this. Here is a no loop version. 

Another advice is to add comments in the code above so that it could be understood better.