SlideServe
Menu
Préférences
© SlideServe · v2026

Mastering Control Flow in MATLAB: Nested Loops and Vectorization Techniques

thwaite-lundy avatar
24 diapositives · 389 Views · Nov 04, 2014

In this lecture, you will explore key concepts of control flow in MATLAB, focusing on nested loops and conditional statements. Learn how to implement and use `continue`, `break`, and `return` commands effectively. The session covers practical examples of nested loops, including the generation of multiplication tables and summation of factorials. Additionally, you'll discover the importance of vectorization for enhancing code performance, including techniques to replace loops with vector operations. Equip yourself with valuable strategies for writing efficient MATLAB code.

Mastering Control Flow in MATLAB: Nested Loops and Vectorization Techniques - slide 1
01 / 24Mastering Control Flow in MATLAB: Nested Loops and Vectorization Techniques
1 / 17

Télécharger la présentation

Mastering Control Flow in MATLAB: Nested Loops and Vectorization Techniques
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. Computer Programming(ECGD2102 ) Using MATLAB Lecture (7): Control Flow (Chapter 2) Control Flow-Cont. Instructor: Eng. Eman Al.Swaity

  2. Objectives (CHAPTER (2):Control Flow) At the end of this lesson, you will be able to understand: • Nested Loops and Nested Conditional Statements. • continue, break, return Commands • Vectorization

  3. Nested Loops and Nested Conditional Statements • Loops and conditional statements can be nested within themselves and each other. • This means that a loop and/or a conditional statement can start (and end) within another loop and/or conditional statement. • Every time a nested loop is typed MATLAB automatically indents the new loop relative to the outside loop.

  4. Nested Loops-Example 1

  5. Nested Loops-Example 1-cont Command Window

  6. Nested Loops-Example 2 fprintf(‘ Demonstrating nested FOR loops.\n\n‘); count=0; for i = 1:5 fprintf(‘ i = %g - j = ‘ , i); for j = 1:10 fprintf(‘ %g ‘, j); count=count+1; end fprintf( ‘ \n ‘ ); end

  7. Nested Loops-Example 2-cont Command Window The value of count is 50.

  8. Nested Loops-Example 3 Multiplication table using nested FOR loops

  9. Nested Loops-Example 3-cont Command Window

  10. Sum of factorials using a nested FOR loop Nested Loops-Example 4

  11. Sum of factorials using a nested FOR loop Nested Loops-Example 4

  12. Nested Loops-Example 4-cont Command Window

  13. continue, break, return • continue – forces current iteration of loop to stop and execution to resume at the start of next iteration. • break – forces loop to exit, and execution to resume at first line after loop. • return – forces current function to terminate, and control to be passed back to the calling function or keyboard.

  14. break Command

  15. Continue Command

  16. Break-Example 1 clc clear all for i=1:10 if i==5 break else fprintf('\nNo(%g)',i) end end Output >> No(1) No(2) No(3) No(4)

  17. Return-Example 1 clc clear all for i=1:10 if i==5 return else fprintf('\nNo(%g)',i) end end Output >> No(1) No(2) No(3) No(4)

  18. Continue -Example 1 clc clear all for i=1:10 if i==5 Continue else fprintf('\nNo(%g)',i) end end Output >> No(1) No(2) No(3) No(4) No(6) No(7) No(8) No(9) No(10)

  19. Comparison of break and return break is used to escape the current while or for loop. return is used to escape the current function.

  20. Vectorization • Vectorizationis the use of vector operations (Matlab expressions) to process all elements of a vector or matrix. Properly vectorized expressions are equivalent to looping over the elements of the vectors or matrices being operated upon. • A vectorized expression is more compact and results in code that executes faster than a non-vectorized expression. • To write vectorized code: • • Use vector operations instead of loops, where applicable • • Pre-allocate memory for vectors and matrices • • Use vectorized indexing and logical functions

  21. Vectorization Non-vectorized code is sometimes called “scalar code” because the operations are performed on scalar elements of a vector or matrix instead of the vector as a whole. Free Advice: Code that is slow and correct is always better than code that is fast and incorrect. Start with scalar code, then vectorize as needed.

  22. Replace Loops with Vector Operations

  23. Pre-allocate Memory

  24. End of the Lecture Let Learning Continue