What is img_w and y_up? How did you get img_w (I'm guessing you used the size() function incorrectly for an image).
So, since the 2-D matrix "edge" has 161 columns in it, why do you think you can stuff 483 elements into it?
Strangely enough 483 is 3 times 161, so it further substantiates my guess you incorrectly did
[img_h, img_w] = size(img);
instead of the more correct:
[img_h, img_w, numberOfColorChannels] = size(img);
So if it's a color image, your img_w would be 3 times what you expect, which is exactly what's happening.