SlideServe
Menu
Préférences
© SlideServe · v2026

Introduction to Programming in MATLAB

nell avatar
7 diapositives · 226 Views · Jul 29, 2014

This presentation by Dr. Cynthia Lee from UCSD explores the use of loops in MATLAB programming. It emphasizes the importance of avoiding repetitive code through loops, scripts, and functions, which not only reduces typing but also minimizes errors and enhances code readability. The lecture discusses the practical applications of loops, such as initializing vectors and performing calculations like sums and averages. Loops allow programmers to run processes repeatedly, making code execution more efficient and flexible.

Introduction to Programming in MATLAB - slide 1
01 / 7Introduction to Programming in MATLAB
1 / 17

Télécharger la présentation

Introduction to Programming in MATLAB
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Introduction to Programming in MATLAB Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Based on a work at www.peerinstruction4cs.org.

  2. LOOPS!

  3. Loops! • Computer programmers are extremely lazy, in that we don’t want to do any more typing than necessary • You’ve already seen that instead of re-typing commands, we can use a script or a function • Not only do we not like re-typing—even copying and pasting lines of code is too much work for us! • Instead of copying and pasting lines of code we want to repeat some number of times, we can use a loop!

  4. for loop Copy-and-paste version for loop version for i = 1:6 myvector(i) = 0 end Of course, you can also do this: myvector(1:6) = zeros(1,6) • myvector(1) = 0 • myvector(2) = 0 • myvector(3) = 0 • myvector(4) = 0 • myvector(5) = 0 • myvector(6) = 0

  5. Software Engineering • Can you think of other reasons (besides laziness) that we might want to use programming constructs like scripts, functions, and loops to avoid re-typing or copying and pasting code? • Here are some suggested reasons: • Less prone to errors in the code • Easier to read the code • Code runs faster • Code is more flexible How many of these are true reasons? • 1 of them (b) 2 of them (c) 3 of them (d) 4 of them

  6. for loop Copy-and-paste version for loop version for i = 1:6 myvector(i) = 0 end Of course, you can also do this: myvector(1:6) = zeros(1,6) • myvector(1) = 0 • myvector(2) = 0 • myvector(3) = 0 • myvecter(4) = 0 • myvector(5) = 0 • myvector(6) = 0

  7. What does this code do? • mystery = 1 • for i = 1:10 mystery = mystery * i end • Computes the sum of the integers from 1 to 8 • Computses the average of the integers from 1 to 10 • Computes the factorial of 10 • Computes the median of the integers from 1 to 10