Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
MATLAB Lesson 8 - For loops     MATLAB Lesson 8 - Logic and Control Lesson index || Introduction | Logic | If-statement | For-loop | Summary Resources Computing lab guide Introduction to MATLAB notes. print this page For-loop For-loops provide the mechanism for repeating a group of statements a fixed number of times. The basic structure of a for-loop is for variable = expression     % Code here to be executed for each value of variable end Given an integer n, calculate the sum of the integers k2 for k = 1,...,n. The variable s is initialised to 0. The index variable k starts at 1, then increases in steps of 1 until it reaches n. Each time through the loop the value of k2 is added to s. >>  s = 0; >>  for k = 1:n             s = s + k^2;         end Given a vector x, calculate the sum its elements. The variable s is initialised to 0. The index variable i starts at 1, then increases in steps of 1 until it reaches the length of the vector x. Each time through the loop the value of x(i) is added to s. >>  s = 0; >>  for i = 1:length(x)             s = s + x(i);         end Given an integer n, write a for-loop to calculate the factorial n!. The variable f is initialised to 1. The index variable k starts at n, then decreases in steps of 1 until it reaches 1. Each time through the loop the value of f is multiplied by the value of k. >>  f = 1; >>  for k = n:-1:1             f = f*k;         end   Vector operations In many cases a for-loop can be replaced by an operation on a vector. This is generally more efficient and also results in briefer code. Two very useful commands are sum which calculates the sum of the elements in a vector, and prod which calculates the product of the elements in a vector. Use the sum function to calculate the average of the values in a vector x. The average is the sum of all the values divided by the number of values. >>  sum(x)/length(x) Use the prod function to calculate n!. Check the values of 6!, 1! and 0! [1:n] is a (row) vector with elements 1, 2, 3, ..., n. The factorial n! is the product of these elements. The product of an empty matrix [1:0] defaults to 1, agreeing with the convention 0! = 1. >>  prod(1:n) >>  prod(1:0)   Warnings If the argument of the of the sum or prod function is a matrix, then the sum or product is taken over each column. Apply the MATLAB functions sum and prod to the matrix A = [1 2 3; 4 5 6] sum calculates the sum of each column producing the row vector [5 7 9] prod calculates the product of each column producing the row vector [4 10 18] >>  A = [1 2 3; 4 5 6] >>  sum(A) >>  prod(A) Use help sum or help prod to get more information. Remember to use semicolons ; to suppress unwanted output during the for-loop.   Self-test Exercise Write a MATLAB for-loop to calculate the first 20 Fibonacci numbers: F1 = 1; F2 = 1;, Fn = Fn-1 + Fn-2 for n = 3,...,20, storing the results in a vector F. Answer: F(1)= 1; F(2) = 1; for n = 3:20     F(n) = F(n-1) + F(n-2); end Use the mouse to select the text between the word "Answer" and here to see the answer. Summary MATLAB uses for loops to execute a group of statements several times. Often a for loop can be replace by a vector operation. Lesson index Next up ---> Summary Copyright © 2019 UNSW Sydney CRICOS Provider Code 00098G