Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See

Slides:



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

Introduction to arrays
Introduction to Matlab
Linear Algebra.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
Maths for Computer Graphics
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
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.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Matrix Mathematics in MATLAB and Excel
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Chapter 10 Review: Matrix Algebra
Graphics CSE 581 – Interactive Computer Graphics Mathematics for Computer Graphics CSE 581 – Roger Crawfis (slides developed from Korea University slides)
1 © 2012 The MathWorks, Inc. Speeding up MATLAB Applications.
Matlab tutorial course Lesson 2: Arrays and data types
Little Linear Algebra Contents: Linear vector spaces Matrices Special Matrices Matrix & vector Norms.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
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,
Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Lecture 28: Mathematical Insight and Engineering.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
OUTLINE Overview Numbers, variables and similar in Matlab
Working with Arrays in MATLAB
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basic Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
Basics Copyright © Software Carpentry 2010 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
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Intro to Matrices Reference: “3D Math Primer for Graphics and Game Development”, chapter 7.1.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
An Introduction to Programming in Matlab Emily Blumenthal
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
Department of Computer Science Western Michigan University
ECE 1304 Introduction to Electrical and Computer Engineering
Linear Algebra review (optional)
Copyright © 2009 Dan Nettleton
Matlab tutorial course
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Communication and Coding Theory Lab(CS491)
Arrays and Matrices in MATLAB
MATLAB Programming Basics Copyright © Software Carpentry 2011
Dr Tripty Singh Arrays.
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
Working with Arrays in MATLAB
Presentation transcript:

Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See for more information. MATLAB Programming

Basics MATLAB ( Facilitates programming problems that require a lot of matrices… And many other things A data parallel programming model –Write x*A*x’ to calculate xAx T –The computer takes care of the loops All encapsulated in special objects called arrays

MATLAB ProgrammingBasics MATLAB arrays are the main data structure in MATLAB. They consist of several variables of the same type, which is usually a double precision, floating point number. They can have one or more dimensions. They can be created, reshaped, indexed, and combined in many ways that we will see below. You’ll hear the terms array and matrix used interchangeably.

MATLAB ProgrammingBasics Create an array from numbers: >> a = [1 2 ; 3 4]; >> a What would “a = [ ]” give you? What about “a = [1; 2; 3; 4]”? A semi-colon denotes a row boundary Columns are delimited by spaces or commas.

MATLAB ProgrammingBasics Create an array from other arrays: >> b = [a a] b Sometimes, this is called a block matrix.

MATLAB ProgrammingBasics Create an array from other arrays: >> b = [a a] b >> b = [b 1]; ERROR! Sometimes, this is called a block matrix.

MATLAB ProgrammingBasics Create special arrays: >> z = zeros(2,3); >> o = ones(3,2); >> e = eye(2,2); % That’s the identity matrix, get it! >> r = rand(2,3); The semi-colon tells MATLAB to suppress output.

MATLAB ProgrammingBasics There are also many functions to operate on arrays. Data-parallel programming tip: it is better to pass an array to a function and have it operate on each element than to pass each element individually. Several functions can be used to examine the shape and type of an array.

MATLAB ProgrammingBasics All operators act on arrays: >> a + a ans = >> a * a ans = What would * do for a 3-dimensional array?

MATLAB ProgrammingBasics All operators act on arrays: >> a + a ans = >> a * a ans = What would * do for a 3-dimensional array? Usually, we’ll ignore this line when we post output. Don’t be alarmed if you see it in your MATLAB session.

MATLAB ProgrammingBasics Other important operators: >> a’ Transpose the array If b is square: >> a / b Equivalent to a * inv(b) If a is square: >> a \ b Equivalent to inv(a) * b inv() is the inverse function for a matrix. More on that later.

MATLAB ProgrammingBasics Operators can also be made to act element wise with a dot: >> a.* b What does a.\ b do? Try to figure out what it will do, then test yourself in MATLAB. a (1,1) b (1,1) a (1,2) b (1,2) a (1,3) b (1,3) a (2,1) b (2,1) a (2,2) b (2,2) a (2,3) b (2,3) a (3,1) b (3,1) a (3,2) b (3,2) a (3,3) b (3,3)

MATLAB ProgrammingBasics Two ways to reshape: >> reshape(a,[4 1]); Resize array into a column vector >> b = a(:); Same thing: make a into a column vector. >> b

MATLAB ProgrammingBasics Reshape reexamined… Let B be a 3x4x5 array. That’s 60 elements. What if we want a 2x3x? array? 2x3x10 = 60, so ? = 10…

MATLAB ProgrammingBasics Or MATLAB can figure out the third parameter: >> B2 = reshape(B,2,3,[]); We used multiple parameters instead of an array. We pass [] for the parameter that should be figured out. Just be sure that there is a shape that works: >> B3 = reshape(B,7,2,[]) Error: 60 is not a multiple of 14.

MATLAB ProgrammingBasics Key question: in what order does reshape consider elements? >>> A A looks 2-dimensional But computer memory is 1-dimensional Must decide how to lay out values

MATLAB ProgrammingBasics Logical Physical Row-major order concatenates the rows Used by C and Python

MATLAB ProgrammingBasics Logical Physical Column-major order concatenates the columns Used by Fortran and MATLAB

MATLAB ProgrammingBasics No difference in usability or performance… …but causes headaches when passing data from one language to another (Just like 0-based vs. 1-based indexing) In what order are 3-dimensional arrays stored?

MATLAB ProgrammingBasics No difference in usability or performance… …but causes headaches when passing data from one language to another (Just like 0-based vs. 1-based indexing) What order are 3-dimensional arrays stored in? reshape always pulls elements in the order they are stored: column major order.

MATLAB ProgrammingBasics Arrays are dynamically resized: >> a(3,3) = What would this do: >> b = 1; >> b = [b 1];

MATLAB ProgrammingBasics What really happens when an array is resized? 1.MATLAB has to allocate a new chunk of memory to hold the array 2.The array is copied to the new memory, with zeros filling the empty spaces. Continuously resizing an array is expensive, so it’s best to initialize an array using >> a = zeros(s1,s2,…) to the largest size that will be needed.

MATLAB ProgrammingBasics Review: Arrays are blocks of homogeneous data They operate like arrays under the standard operations +,-,*. MATLAB provides many methods to work with arrays Reshape Resize …

February 2011 created by Richard T. Guy Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See for more information.