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:

  1. clear b @ start of program or else previous calculation effect current
  2. matlab vector system, when did 1:n made vector such [1 2 3 4], when made for loop 1:(1:n) confusing @ best. should 1:n.
  3. not sure why need variable called t
  4. sum should replaced standard + operation.
  5. don't forget x go last number specific, , hence should avoid adding n

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -