Matlab tutorial course Lesson 2: Arrays and data types

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.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
SOLVING SYSTEMS OF LINEAR EQUATIONS. Overview A matrix consists of a rectangular array of elements represented by a single symbol (example: [A]). An individual.
Refresher: Vector and Matrix Algebra Mike Kirkpatrick Department of Chemical Engineering FAMU-FSU College of Engineering.
3_3 An Useful Overview of Matrix Algebra
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Yuval Hart, Weizmann 2010 © 1 Introduction to Matlab & Data Analysis Tutorial 2 and 3: Variables and Matrix Manipulation.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
Eilon Sharon, Weizmann 2008 © 1 Introduction to Matlab & Data Analysis Tutorial 2: Variables and Matrix Manipulation.
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.
Week 3 Last week Vectors Array Addressing Mathematical Operations Array Multiplication and Division Identity Matrix Inverse of a Matrix Element by Element.
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.
Lecture 7: Matrix-Vector Product; Matrix of a Linear Transformation; Matrix-Matrix Product Sections 2.1, 2.2.1,
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.
Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory.
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.
Chapter 10 Review: Matrix Algebra
ECON 1150 Matrix Operations Special Matrices
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
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,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
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.
Lesson 11-1 Matrix Basics and Augmented Matrices Objective: To learn to solve systems of linear equation using matrices.
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
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.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
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.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
1 Matrix Math ©Anthony Steed Overview n To revise Vectors Matrices.
MTH108 Business Math I Lecture 20.
Linear Algebraic Equations and 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,
Chapter 7 Matrix Mathematics
Linear Algebraic Equations and Matrices
Matlab tutorial course
Introduction to MATLAB [Vectors and Matrices] Lab 2
MATLAB Programming Basics Copyright © Software Carpentry 2011
Matrices in MATLAB Dr. Risanuri Hidayat.
Math review - scalars, vectors, and matrices
Presentation transcript:

Matlab tutorial course Lesson 2: Arrays and data types

… 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… …. the value of each pixel represents its intensity (or colour) We call each number a pixel (or in 3D a voxel)…

… 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

1D Arrays: vectors Creating – Explicitly: a = [ ]; a = [1; 2; 3; 4]; a = [ ] – 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 A = [ ; 5, 6, 7, 8]; 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 B C

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); 3 rd column of the 2 nd row – giving a single index b = A(10) We can access whole rows or columns using : b = A(2,:); b is 1x4, the 2 nd row of A c = A(:,4); c is 4x1, the 4 th column of A We can access specific ranges b = A(1, 2:4); b is 1x3, the 2 nd, 3 rd and 4 th elements of the 1 st 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 1 st and 4 th elements of the 2 nd and 3 rd 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 2 nd to 5 th elements of the 1 st row c = A(2:end-1,3); c is 2x1, the 3 rd column of the 2 nd to 3 rd 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 4 th row of B to its 1 st row (the values of which are now lost!) – Or with a scalar A(:,2) = 0; Sets the 2 nd column of A to zero B([1 4 15]) = pi; Sets the 1 st, 4 th and 15 th 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-bands 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