Matlab tutorial course

Slides:



Advertisements
Similar presentations
Introduction to MATLAB The language of Technical Computing.
Advertisements

Introduction to Matlab
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
3_3 An Useful Overview of Matrix Algebra
Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) matrix operations circuit analysis using Matlab image processing – simple examples Chapter.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
Pam Perlich Urban Planning 5/6020
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
1 Chapter 2 Matrices Matrices provide an orderly way of arranging values or functions to enhance the analysis of systems in a systematic manner. Their.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matlab tutorial course Lesson 2: Arrays and data types
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Lecture 28: Mathematical Insight and Engineering.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
MT411 Robotic Engineering Asian Institution of Technology (AIT) Chapter 1 Introduction to Matrix Narong Aphiratsakun, D.Eng.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Linear System of Simultaneous Equations Warm UP First precinct: 6 arrests last week equally divided between felonies and misdemeanors. Second precinct:
Matrix Algebra Basics Chapter 3 Section 5. Algebra.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
MTH108 Business Math I Lecture 20.
Linear Algebra Review.
Eigenfaces (for Face Recognition)
Linear Algebraic Equations and Matrices
CGN 2420 Matrix Operations in Excel
MATRICES.
Linear Algebra review (optional)
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
응용 전산 및 실습 MATLAB – Chapter 3 행렬연산
Chapter 7 Matrix Mathematics
L – Modeling and Simulating Social Systems with MATLAB
Chapter 3 Arrays and Vectors
Linear Algebraic Equations and Matrices
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
7.3 Matrices.
MATH 493 Introduction to MATLAB
Vectorized Code, Logical Indexing
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
Matlab tutorial course
INTRODUCTION TO MATLAB
MATLAB Programming Basics Copyright © Software Carpentry 2011
Multi-Dimensional Arrays
Matlab Training Session 2: Matrix Operations and Relational Operators
Linear Algebra review (optional)
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Matrices in MATLAB Dr. Risanuri Hidayat.
Math review - scalars, vectors, and matrices
Matrix Operations Ms. Olifer.
Working with Arrays in MATLAB
Linear Algebra and Matrices
Presentation transcript:

Matlab tutorial course michael.berks@manchester.ac.uk Lesson 2: Arrays and data types

Those tutorials were dull… … I want to do something fun with images! Those tutorials were dull…

What does an image look like to a computer? A computer ‘sees’ an image as an array of numbers… We call each number a pixel (or in 3D a voxel)… …. the value of each pixel represents its intensity (or colour) liver_slice = imread(‘data\liver_slice.png’);

… I need to understand how to use arrays So to learn how to do stuff with images…

Lesson overview Arrays: vectors, matrices and higher dimensions Creating Indexing and manipulating Standard mathematical operations adding, multiplying etc. Element-wise vs matrix calculations Other data types: Cells, structures, strings Numerical types: Integers, doubles, logicals

Arrays Matlab was originally designed to allow efficient and intuitive processing of vectors and matrices Understanding how to use arrays is the key to successfully working with Matlab Next week we’ll see more on how images in Matlab are just arrays of numbers

Python users Need to import numpy You may find it useful to have this tutorial open https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

1D Arrays: vectors Creating Explicitly: Using the ‘:’ operator: a = [1 2 3 4]; a = [1; 2; 3; 4]; a = [0.2 3 2.7] Using the ‘:’ operator: a = 1:4; a = 4:1; a = 4:-1:1; a = 1:10:40; a=0:0.2:1; Using ‘ones’ or ‘zeros’ a = ones(5,1); a = ones(1,5); a = zeros(10,1); Using random sampling functions ‘rand’, ‘randn’ a = rand(5,1); a = randn(5,1);

2D Arrays: matrices Creating: Explicitly Use a space or , for each new column Use ; for each new row Using ‘ones’, ‘zeros’, ‘rand’ etc A = rand(5,5); A = rand(6); Combining other vector and matrices A = rand(2,3); B = ones(1,3); C = zeros(3,2); D = [ [A; B] C]; Reshaping a vector A = reshape(1:16, 4, 4); display(A); what does this tell us about the order elements are assigned? A C B

Indexing arrays All arrays are indexed row then column A = reshape(1:16, 4, 4); We can access individual elements by giving row and column subscripts b = A(2, 3); 3rd column of the 2nd row giving a single index b = A(10) We can access whole rows or columns using : b = A(2,:); b is 1x4, the 2nd row of A c = A(:,4); c is 4x1, the 4th column of A We can access specific ranges b = A(1, 2:4); b is 1x3, the 2nd, 3rd and 4th elements of the 1st row C = A(2:3,2:3); C is 2x2, the middle 4 elements of A D = A(2:3, [1 4]); D is 2x2, the 1st and 4th elements of the 2nd and 3rd rows

Indexing arrays (cont.) When accessing arrays, ‘end’ acts a special (and very useful!) keyword that refers to the size of that dimension A = rand(4,5); b = A(1, 2:end); b is 1x4, the 2nd to 5th elements of the 1st row c = A(2:end-1,3); c is 2x1, the 3rd column of the 2nd to 3rd rows d = A(1:end/2,:); d is 2x5, all columns of the first 2 rows

Indexing arrays - assigning We can also use indexing on the LHS of equations to assign parts of an array A = zeros(5,5); B = rand(4,4); Either with an array of the same size A(1:4,1:4) = B; A(3:5,4:5) = B(1:3,1:2); A(3:5,4:end) = B(1:3, [1 4]); A([1 5 9]) = B([2 4 11]); A(1:4,1:3) = B; Causes an error! Why? Copy back to the same variable B(1,:) = B(4,:); Copies the 4th row of B to its 1st row (the values of which are now lost!) Or with a scalar A(:,2) = 0; Sets the 2nd column of A to zero B([1 4 15]) = pi; Sets the 1st, 4th and 15th elements of B to π

Out-of-bounds indexing If we try and access an element using an index larger than the dimensions of an array, we get an out-of-bounds error A = rand(5,5); b = A(5,6); b = A(26); b = A(end+1,2); If we assign to out-of-bounds indices, Matlab automatically extends the target array A(5,6) = 1; A is now 5x6 A(end+3,2) = 2; A is now 7x6 A(:,end+1) = rand(7,1); A is now 7x7 Negative numbers, zeros, and non-integers can never be used, either accessing or assigning (Except in the special case of logical indexing – we’ll cover later!)

Exercise Go to my website (did you bookmark it last week?) Save the script ‘array_indexing.m’ into your Matlab scripts folder from Open the script Copy and paste each line (or highlight and press F9) to run each line at the command line Do all the examples make sense? Which lines generate errors? Why?

Vector & Matrix Algebra Standard mathematical operators apply the rules of linear algebra if A is n x m and B is p x q C = A*B; is only valid when m = q (C will be n x q) OR either A or B are scalar (1x1) C = A+B; is only valid when n=m and p=q OR either A or B are scalar A^2; means A*A, is only valid if A is square A’; or ctranspose(A); is the transpose operator Takes conjugate of complex values A^-1; or inv(A); is the inverse operator only valid if A is square

Element-wise operations Sometimes it is useful to have 1D an 2D arrays that are just collections of numbers (like images!) We don’t want to apply the rules of linear algebra We want to apply the same operation to every element We can use element-wise operations that apply a scalar operation to each element C = A.*B; means C(i,j) =A(i,j)*B(i,j) Also C=A./B; C=A.^2 Try A = rand(3); B = A^0.5 B = sqrt(A) B = A.^0.5

Summary Understanding how arrays work is the key to successful programming [] – square brackets create new arrays () – access parts of an array +,*,^ etc. perform linear algebra (matrix) operations .+,.*,.^ etc. perform element-wise operations Next lesson: other data types