Skip to content

This book guides the reader from MATLAB basics to advanced applications through graded examples. It covers variables, arrays, control flow, matrix traversal, matrix operations, functions, recursion, and object handling. Advanced topics include statistical measures, signal processing, sequence alignment, randomness, and Markov chains.

License

Notifications You must be signed in to change notification settings

Gagniuc/MATLAB-Coding-Examples-from-Simple-to-Complex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MATLAB Coding Examples from Simple to Complex

This book guides the reader from MATLAB fundamentals to advanced applications through a carefully graded sequence of examples. It opens with an introduction on MATLAB’s future and platform particularities, then builds core fluency with variables, assignments, formatted I/O, and arithmetic. Control flow is developed through conditional branching and a wide range of loop patterns, after which arrays are treated as first-class data structures, from basic indexing and updates to mapping, aggregation, and small visualizations. Multidimensional traversal and matrix operations extend these skills to practical linear-algebra routines and region scans, including flips, rotations, diagonals, and mapped computations. Functions are introduced systematically (from simple procedures to multi-parameter routines), followed by composition, chaining, purity, globals, and recursion. Object construction and method use are complemented by JSON interchange and transformations between objects, 1D arrays, and matrices. Moderate examples consolidate technique with string-to-array loaders, matrix utilities, logical operations, native sorting and deduplication, permutations, statistical measures (averages, dispersion, correlation, entropy), and text/hex/Base64 conversions. Complex examples showcase signal processing, logic-gate mappings on matrices, sequence alignment, and island counting with coverage metrics. A final part addresses randomness, stochastic selection, and Markov-chain simulators, while the MATLAB-specific chapter focuses on figures and charts, GUI construction, file I/O, and built-in encoders/decoders. Throughout, the content remains native to MATLAB, emphasizing clear patterns that scale from simple demonstrations to robust solutions.

Note: The repositories below showcase a few demo algorithmic implementations drawn from the total of 226 algorithms presented in Coding Examples from Simple to Complex - Applications in MATLAB. Synthesis Lectures on Computer Science (Springer International Publishing, 2024). You may find these standalone implementations insightful:


Note: The MATLAB edition of this book is officially featured by MathWorks, the company behind MATLAB, on their website:
Coding Examples from Simple to Complex – Applications in MATLAB


Coding Examples from Simple to Complex – Applications in MATLAB

Table of Contents (Matlab)

1. Introduction

  • 1.1 Future of Matlab
  • 1.2 The content is native
  • 1.3 MATLAB particularities

2. Variables

  • Ex. (1) – Commenting inside code
  • Ex. (2) – Naming variables
  • Ex. (3) – Write your first Matlab program
  • Ex. (4) – The meaning of "a = b"
  • Ex. (5) – Assign and reassign with type change
  • Ex. (6) – Basic mathematical operations
  • Ex. (7) – The meaning of modulo operator
  • Ex. (8) – The meaning of "a = a + 1"
  • Ex. (9) – The meaning of "a = a - 1"
  • Ex. (10) – The plus operator
  • Ex. (11) – The minus operator
  • Ex. (12) – Variable playground
  • Ex. (13) – Swap values
  • Ex. (14) – Empty a variable
  • Ex. (15) – Line continuation
  • Ex. (16) – Formatted output

3. Conditional Branching

  • Ex. (17) – If then else – conditional statements (I)
  • Ex. (18) – If then else – conditional statements (II)
  • Ex. (19) – If then else - conditional statements (III)
  • Ex. (20) – If then elseif else - conditional statements
  • Ex. (21) – Switch - conditional statements

4. Loops

  • Ex. (22) – While loop
  • Ex. (23) – Do while
  • Ex. (24) – Simple for loop
  • Ex. (25) – Reverse by subtraction from the upper limit
  • Ex. (26) – The meaning of steps
  • Ex. (27) – The immutable counter variable
  • Ex. (28) – Reverse by subtraction from the upper limit variable
  • Ex. (29) – For loop summation
  • Ex. (30) – Simple counter summation
  • Ex. (31) – Sum all results of addition of 1 in a n×n cycle
  • Ex. (32) – Sum all results of addition of 3 in a n×n cycle
  • Ex. (33) – Sum all results of the multiplication between i and j
  • Ex. (34) – Nested for loops and summation of counter variables
  • Ex. (35) – Nested for loops and summation based on the inner counter
  • Ex. (36) – Nested for loops & summation based on counters and upper limits (I)
  • Ex. (37) – Nested for loops & summation based on counters and upper limits (II)
  • Ex. (38) – Nested for loops & summation based on counters and upper limits (III)
  • Ex. (39) – Show i and j coordinates at each step
  • Ex. (40) – One for loop that simulates two for loops

5. Arrays

  • Ex. (41) – Array addition and concatenation
  • Ex. (42) – Extracting individual values from the elements of an array
  • Ex. (43) – Adding elements
  • Ex. (44) – Using array literals of different data type
  • Ex. (45) – Accessing array elements
  • Ex. (46) – Changing values in array elements - swap values or replace
  • Ex. (47) – Extracting individual values from the elements of an array
  • Ex. (48) – Array length
  • Ex. (49) – Accessing the values from the components of an array
  • Ex. (50) – Traverse a 1D array using a while loop
  • Ex. (51) – Traverse a 1D array using a for loop (I)
  • Ex. (52) – Traverse two 1D arrays using a for loop (II)
  • Ex. (53) – Print different data type values from array using a for loop (I)
  • Ex. (54) – Print all integers from array using a for loop (II)
  • Ex. (55) – Sum all values from array
  • Ex. (56) – Multiplication involving a scalar and a 1D array
  • Ex. (57) – Insert values into an array
  • Ex. (58) – Insert ascending and descending integer values into arrays
  • Ex. (59) – Add forward and reverse values and subtract max
  • Ex. (60) – Pointless equilibrium
  • Ex. (61) – Max value from array
  • Ex. (62) – Min value from array
  • Ex. (63) – Max value above two arrays of the same size
  • Ex. (64) – Max value above two arrays of different sizes
  • Ex. (65) – Which is bigger between n and n + 1?
  • Ex. (66) – Which is bigger between n and n + 1? (optimization)
  • Ex. (67) – Sum two arrays
  • Ex. (68) – Simple array mapping
  • Ex. (69) – Sum by coordinates (I)
  • Ex. (70) – Sum by coordinates (II)
  • Ex. (71) – Cutoff value
  • Ex. (72) – Swap array elements by pattern
  • Ex. (73) – Mix array based on pattern
  • Ex. (74) – Swap array values
  • Ex. (75) – Intermittent value swap
  • Ex. (76) – Reverse string
  • Ex. (77) – The welding of array values
  • Ex. (78) – Static modulo - fill up array with modulo results
  • Ex. (79) – Dynamic modulo - take a(i) modulo j
  • Ex. (80) – Convert a string to an array
  • Ex. (81) – The rule of three simples
  • Ex. (82) – Average, standard deviation and coefficient of variation
  • Ex. (83) – Horizontal chart from ASCII characters
  • Ex. (84) – Horizontal chart with bars proportional with max from array
  • Ex. (85) – Horizontal chart with UTF characters proportional with max array

6. Traversal of Multidimensional Arrays

  • Ex. (86) – Accessing the elements of matrix A
  • Ex. (87) – Accessing the elements of matrix A using nested for loops
  • Ex. (88) – Traverse a matrix with a single for loop (I)
  • Ex. (89) – Traverse a matrix with a single for loop (II)
  • Ex. (90) – Accessing the elements of a 3D array
  • Ex. (91) – Traverse a 3D object with a single for loop
  • Ex. (92) – Traverse a 2D object with a single for loop and integer division
  • Ex. (93) – Traverse a 2D object with a single for loop using arithmetic operators
  • Ex. (94) – 3D traversal with one for-loop using only arithmetic operators

7. Matrix Operations

  • Ex. (95) – How many 1's in matrix
  • Ex. (96) – Sum all values from matrix elements
  • Ex. (97) – Show matrix content
  • Ex. (98) – Multiplication involving a scalar and a matrix
  • Ex. (99) – Sum all values from the rows of the matrix
  • Ex. (100) – Sum all values from the columns of the matrix
  • Ex. (101) – Find max and min on columns and rows of a matrix
  • Ex. (102) – Multiply all values from the columns / rows and store them in array
  • Ex. (103) – Sum all values from the right diagonal of the square matrix
  • Ex. (104) – Sum all values from the left diagonal of the square matrix
  • Ex. (105) – Sum all values from the left and right diagonal of a square matrix
  • Ex. (106) – Sum all values from the left and right diagonal by using conditions
  • Ex. (107) – Show bottom–left part of the matrix
  • Ex. (108) – Show bottom–left part of the matrix and flip horizontal
  • Ex. (109) – Show top–right part of the matrix
  • Ex. (110) – Show top–right part of the matrix and flip 90 degrees left
  • Ex. (111) – Show top–right, flip 90 degrees left and flip horizontally
  • Ex. (112) – Secondary diagonal scan (right)
  • Ex. (113) – Secondary diagonal scan (left)
  • Ex. (114) – Show bottom–right and flip horizontally
  • Ex. (115) – Matrix flip vertical
  • Ex. (116) – Matrix flip horizontal
  • Ex. (117) – Sum values from elements of a matrix based on a map
  • Ex. (118) – Add two matrices into a third
  • Ex. (119) – Matrix multiplication with three for loops
  • Ex. (120) – Matrix multiplication with two for loops
  • Ex. (121) – Multiply specific elements of two matrices based on a map
  • Ex. (122) – Different operations based on maps
  • Ex. (123) – Different operations based on maps (SMC)
  • Ex. (124) – Nested arrays

8. Functions

8.1 Built-in Functions/Methods

  • Ex. (125) – Built-in Sin, Exp
  • Ex. (126) – Max between two integer variables
  • Ex. (127) – Max between two specific elements of an array
  • Ex. (128) – Max over the values from an array
  • Ex. (129) – Max over two array variables
  • Ex. (130) – Random integers from 0 to 99 in an array
  • Ex. (131) – Insert random values in the elements of a matrix
  • Ex. (132) – Split string to integers by using a delimiter symbol
  • Ex. (133) – Split string to array by using the "|" symbol
  • Ex. (134) – Cascading built-in functions (strrep, length)
  • Ex. (135) – Built-in sort function

8.2 Making of Functions

  • Ex. (136) – Making of a function
  • Ex. (137) – Making of a function with more than one parameter
  • Ex. (138) – Gauss summation - sum all from 0 to n
  • Ex. (139) – Function calls to other functions
  • Ex. (140) – A simple scanner to find the output distribution
  • Ex. (141) – Function chaining - nested function calls
  • Ex. (142) – Function composition
  • Ex. (143) – Global variables and constants
  • Ex. (144) – Pure and impure functions
  • Ex. (145) – Procedures vs Functions

8.3 Recursion

  • Ex. (146) – Replacement for repeat loops with recursion
  • Ex. (147) – Repeat string n times recursively
  • Ex. (148) – Sum from 0 to n recursively
  • Ex. (149) – Factorial from 0 to n
  • Ex. (150) – Generate a sequence recursively
  • Ex. (151) – Generate fibonacci recursively
  • Ex. (152) – Sum all from array recursively

9. Objects

9.1 Constructors And Methods

  • Ex. (153) – Using an object constructor
  • Ex. (154) – An object with three properties and a method (I)
  • Ex. (155) – An object with three properties and a method (II)
  • Ex. (156) – An object with complex methods
  • Ex. (157) – Generate multiple objects with methods

9.2 JSON

  • Ex. (158) – Object to JSON
  • Ex. (159) – JSON to Object
  • Ex. (160) – Anything to object to string
  • Ex. (161) – Complex string to object (I) - direct nested access
  • Ex. (162) – Complex string to object (II) - nested access by reference
  • Ex. (163) – Make 1D array from parts of an object
  • Ex. (164) – Make a matrix from parts of an object

10. Moderate Examples

10.1 Load Arrays from Strings

  • Ex. (165) – Strings to 1D arrays (I)
  • Ex. (166) – Strings to 1D arrays (II)
  • Ex. (167) – A 2D array loaded from string
  • Ex. (168) – Load a matrix from a string by using two delimiters
  • Ex. (169) – A function to correctly display a matrix
  • Ex. (170) – A function to load and display matrices
  • Ex. (171) – Load two matrices from strings and make the addition

10.2 Some Matrix Operations

  • Ex. (172) – Function to swap diagonal of matrix
  • Ex. (173) – Function to transpose a matrix
  • Ex. (174) – Function for rotation of a matrix by 90 degrees

10.3 Logical Operations

  • Ex. (175) – Logical NOT
  • Ex. (176) – Logical AND
  • Ex. (177) – Logical OR
  • Ex. (178) – Logical NAND (NOT AND)
  • Ex. (179) – Logical NOR (NOT OR)
  • Ex. (180) – Logical XOR
  • Ex. (181) – Logical XNOR

10.4 Miscellaneous

  • Ex. (182) – Logarithm of b in base a
  • Ex. (183) – Smooth signal
  • Ex. (184) – Greatest common divisor (GCD)
  • Ex. (185) – Pseudo random generator
  • Ex. (186) – Double brute force algorithm (DBFA)
  • Ex. (187) – Alphabet detection
  • Ex. (188) – Alphabet detection on matrices

10.5 Sorting

10.6 Permutations

  • Ex. (192) – Get all permutations of a given string (I)
  • Ex. (193) – Get all permutations of a given string (II)

10.7 Statistics

10.8 Useful Conversions

  • Ex. (200) – Text (txt) to hexadecimal (hex)
  • Ex. (201) – A txt to hex from array a to array b
  • Ex. (202) – A txt to hex with in-place replacement
  • Ex. (203) – A txt to hex in a function that receives an as argument
  • Ex. (204) – Multiple functions for any to any conversion
  • Ex. (205) – One function for any to any conversion and input type detection
  • Ex. (206) – Base64 encoding function

11. Complex Examples

12. Randomness and Programming

  • Ex. (215) – Get complementary array by using random values
  • Ex. (216) – Take the first 20% of the closest solutions (mutation/selection)
  • Ex. (217) – Find complementary matrix by using stochastic means
  • Ex. (218) – A two states Markov Chain simulator based on letters
  • Ex. (219) – A two states Markov Chain simulator based on probability values
  • Ex. (220) – Multiply a probability vector with a probability matrix n times
  • Ex. (221) – A Markov Chain framework for simulation

13. MATLAB Specific

  • Ex. (222) – Base64 encoding and decoding via built-in functions
  • Ex. (223) – A kind of local storage
  • Ex. (224) – File I/O
  • Ex. (225) – Figures and Charts
  • Ex. (226) – The Graphical User Interface (GUI)

14. References


Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in MATLAB. Synthesis Lectures on Computer Science. Springer International Publishing, 2024, pp. 1-255.


Coding Examples from Simple to Complex

About

This book guides the reader from MATLAB basics to advanced applications through graded examples. It covers variables, arrays, control flow, matrix traversal, matrix operations, functions, recursion, and object handling. Advanced topics include statistical measures, signal processing, sequence alignment, randomness, and Markov chains.

Topics

Resources

License

Stars

Watchers

Forks