MATLAB Class 2 Some examples and images are

MATLAB Class 2 Some examples and images are taken from Matlab Academy and Math. Works. com

Syllabus: main points 1. Introduction 2. Matrices and Vectors 3. Matrices manipulations (1 st part) 4. Relational & logic operators, If, Else 5. Loops 6. Practice! 7. Debugging 8. Useful stats + project intro 9. import files + project 10. Visualizations (project) 11. Functions

Take the red pill And follow me into the Matrices… In MATLAB

Arrays • Virtually, all MATLAB variables are arrays. • You can use arrays to store data in one variable.

From last class… • A single number, called a scalar, is a 1 -by-1 array. e. g. , x=2 • You can create arrays with multiple elements using [ ] X = [1 23] x= 1 23

Row vs column vectors • When you separate numbers by spaces (or commas) x = [1 2 3] or x = [1, 2, 3] MATLAB combines the numbers into a row vector, which is an array with one row and multiple columns (1 -by-n). • When you separate numbers by semicolons, MATLAB creates a column vector (n-by-1). x = [1; 3] • x= 1 3

The Matrix • combine spaces and semicolons to create a matrix • When entering a matrix, you must enter them row by row. x = [3 4 5; 6 7 8] x= 345 678

Operations with vectors and matrices You can add a scalar value to all the elements of an array. Pretty time-saving, right? x = [2 3 4]; y = x + 10 y= 12 13 14 You can also multiply or divide all of the elements of an array by a scalar. z = 2*x y = x/3

Matrix multiplication • The * operator may produce an error message. • the. * operator performs elementwise multiplication and allows you to multiply the corresponding elements of two equally sized arrays.

• You can add together any two arrays of the SAME SIZE. Check Compatible Array Sizes for Basic Operations to learn more about it!

Some useful functions

zeros()

ones()

size()

length()

Rand()

From Rand() to Randi()

Randi () Produce a matrix of random number, all integers (e. g. , 2, 5, not 0. 87). Randi (max value, numb_rows, numb_columns) x = randi(15, 4, 5) A matrix 4 by 5 with random numbers from 1 to 15.

Let’s practice! • Open the script “lesson 2. m” Have fun! (Go through the exercise alone until the script says “End first part, wait”. UP TO THE “VECTOR” SECTION –EXCLUDED-)

Did the “Boring Task” bored you? For long vectors, entering individual numbers is not practical. Use the : operator and specify only the start and end points. Quick_vector = 1: 20 Quick_vector = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

The : operator uses a default spacing of 1, but you can specify your own spacing. I_decide_my_own_spacing = 10: 2: 20 I_decide_my_own_spacing = 10 12 14 16 18 20

Transpose operator ‘ • you can convert a row vector into a column vector using the transpose operator (‘). • You can transpose x, while creating it. X = 4: 8 X = (4: 8)’ X= x= 45678 Y = x’ y=4 5 6 7 8

Linspace • Handy function if you know the number of elements you want in a vector • Linspace() Go back to the matlab file “lesson 2. m” in the section “vectors” to find out more! (stop before the section “vector indexing”).

How to access a Matrix or a vector?

Indexing

Index




X (Row, Column)



Break rooms! • About 3 people per group. • From “vector indexing” section, until the end.

Bonus exercises (lesson 3. m file)
![Selecting non-consecutive numbers • V = [3, 5, 6, 7, 12, 67] %selecting element Selecting non-consecutive numbers • V = [3, 5, 6, 7, 12, 67] %selecting element](http://slidetodoc.com/presentation_image_h2/20bee8a5ce74d449e0d876c41d4e93c0/image-35.jpg)
Selecting non-consecutive numbers • V = [3, 5, 6, 7, 12, 67] %selecting element in the positions: 2 nd 5 th , 6 th v([2, 5, 6]) You need to use the [ ] inside the ( )




Syllabus: main points 1. Introduction 2. Matrices and Vectors 3. Matrices manipulations (2 nd part) 4. Relational & logic operators, If, Else 5. Loops 6. Practice! 7. Debugging 8. Useful stats + project intro 9. import files + project 10. Visualizations (project) 11. Functions

Questions?
- Slides: 40