Let's have a look at the line that results in an error:
Can you spot the error? I can't. So you have to split it into smaller parts to see what array sizes you're dealing with.
So the error might already be here if N is not 10.
The issue might also be with the division. You also use length instead of size or numel, which should be avoided.
Try saving each part as a separate variable to find the exact source of the problem. You also need to load to a struct to make it clear where each variable comes from.
There is also a more fundamental problem: you call mean without an argument.