Getting the error. "index exceeds the number of array elements".
12 views (last 30 days)
Steven Lord on 30 Jul 2022
When i is 2, you assign to h(2) but then on the next line you attempt to access h(3). That element doesn't exist yet.
I suggest moving the second line of your loop to after the loop and removing its dependence on the loop variable.
x = (1:10).^2
y = x(2:end)+x(1:end-1)
Note that y has one fewer element than x does, so you'll need to handle the edges.