Well, the problem is obvious. You're essentially doing
Since sin is a real number between -1 and 1, there is absolutely no guarantee that r(2) will be an integer. Thus, you can't use it as a pixel index. You could round it but I'm not sure you could reverse the process (to get the original image back).
Note that the original code and therefore your modifications are not very good. Variable names are extremely bad (should be just add a 'g' for the next variable?), they have no meaning. Also, the code queries the number of columns of the image, but then completely ignores it and goes on to assume that the image is square. This will result in an indexing error if the image is taller than wide.
Other silliness: g = double(g)/255, g is already made double in the previous line. In any, case the whole lot can be replaced by a call to im2double. The subplot syntax used is also undocumented.
sourceimage = im2double(imread('rice.png'));
assert(ndims(sourceimage) == 2, 'Image is not greyscale');
[height, width] = size(sourceimage);
Kfactor = 6;
scrambledimage = zeros(size(sourceimage));
for column = 1:width
for row = 1:height
sourcepixel = mod([row + column, round(column + Kfactor*sin(((row+1)*width)/2*pi))], [height, width]);
scrambledimage(row, column) = sourceimage(sourcepixel(1) + 1, sourcepixel(2) + 1);
imshowpair(sourceimage, scrambledimage, 'montage');