Write a program that uses the FOR loop to sum all even integers less than N (MATLAB) -
what did
n = input ('n='); x = 1:n x= (1:n) if mod(x,2) == 0 t = x; b = sum(t) end end is correct?
why keep giving me error message?
"??? index exceeds matrix dimensions.
error in ==> exampractise1 @ 7
b = sum(t)"
n = input ('n='); b=0; x= (1:n-1) if (mod(x,2) == 0) b=b+x; end end disp(b); a few points:
- clear
b@ start of program or else previous calculation effect current - matlab vector system, when did
1:nmade vector such[1 2 3 4], when madeforloop1:(1:n)confusing @ best. should1:n. - not sure why need variable called
t sumshould replaced standard+operation.- don't forget
xgo last number specific, , hence should avoid addingn
Comments
Post a Comment