l2norm = 0.0; total = 0.0; for i = 1:length(pgl) l2norm = l2norm + (pgl(i)-pgh((i-1)*2+1))^2.0 ; total = total + pgh((i-1)*2+1)^2.0; index must not exceed 46. in matlab

You can add an if statement to stop the loop when i reaches the value of 46 as follows:

main.m
l2norm = 0.0;
total  = 0.0; 
for i = 1:length(pgl)   
    if i > 46
        break
    end
    l2norm = l2norm + (pgl(i)-pgh((i-1)*2+1))^2.0 ;    
    total = total + pgh((i-1)*2+1)^2.0;
end
190 chars
10 lines

This will break the loop when the value of i is greater than 46.

related categories

gistlibby LogSnag