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:n
made vector such[1 2 3 4]
, when madefor
loop1:(1:n)
confusing @ best. should1:n
. - not sure why need variable called
t
sum
should replaced standard+
operation.- don't forget
x
go last number specific, , hence should avoid addingn
Comments
Post a Comment