Suppose you were a pirate being forced to wash the plank that your pirate crew uses to force people to walk the plank. The plank is six paces long, so you clean then step forward. But what you don't realize is that the plank has rotted a bit, so as soon as you step onto it the last pace of the board falls off. So you wash, step forward, wash, step forward, and repeat this process. You wash the fifth pace of the plank and go to take a step forward ... but that sixth pace of plank is no longer there!
That's effectively what you're doing here. I've wrapped this example in try / catch so I can run both this example (which won't work) and the next (which will) in the same answer.
fprintf("Washed element " + plank(k) + newline)
fprintf("Element " + (7-k) + " fell off!" + newline)
disp("The operation threw the following error: " + newline + theError.message)
In this case the plank starts off with six elements, then five, then four, and finally when you try to wash element four the plank only has three elements.
You could resolve this by marking in a separate array which elements get deleted at each step and deleting them all at once after the loop is complete.
toDelete = false(size(values));
for ind = 1:numel(values)
fprintf("Element %d (%d) is prime. Marking for deletion." + newline, ind, values(ind))