Do not use the name "sum" for a variable. Avterwards the function with the same name is shadowed:
So simply rename your variable "sum":
If your code is a script, "sum" has been shadowed in the base workspace. Then clean it up by:
A general hint: Use functions instead of scripts, such that shadowed functions do not have unexpected effects in other scripts, where they are extremly confusing. Shadowing important built-in function is a frequent problem, so avoid this as good as possible.
Are you sure, that this is wanted:
[rows , columns] = size(video(i));
noisyvideo(i) = imnoise(video(i),"gaussian",0,0.02);
the array noisyvideo is growing in each iteration. The editor should show you a hint. This is extremly expensive. Example:
Although the resulting vector needs 8MB only (8 bytes per double), Matlab has to create a new vector in each iteration and copies the elements of the former ones. So in totel Matlab allocates sum(1:1e6)*8 bytes and copys almost the same amoubnt of memory: More than 4 Terabyte!
Solution: Either preallocate:
Or if you do not need all x, do not store them:
In your case I guess using noisyvideo without (i) is sufficient.