Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons...
-
Upload
dorthy-bennett -
Category
Documents
-
view
221 -
download
3
Transcript of Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons...
![Page 1: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/1.jpg)
Basics
Copyright © Software Carpentry 2011
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
MATLAB Programming
![Page 2: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/2.jpg)
MATLAB Programming Basics
MATLAB (http://www.mathworks.com)
Facilitates programming problems that require a lot of matrices…
And many other things
A data parallel programming model
– Write x*A*x’ to calculate xAxT
– The computer takes care of the loops
All encapsulated in special objects called arrays
![Page 3: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/3.jpg)
MATLAB Programming Basics
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.
![Page 4: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/4.jpg)
MATLAB Programming Basics
Create an array from numbers:
>> a = [1 2 ; 3 4];
>> a
1 2
3 4
What would “a = [1 2 3 4]” give you?
What about “a = [1; 2; 3; 4]”?
A semi-colon denotes a row boundary
Columns are delimited by spaces or commas.
![Page 5: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/5.jpg)
MATLAB Programming Basics
Create an array from other arrays:
>> b = [a a]
b
1 2 1 2
3 4 3 4
Sometimes, this is called a block matrix.
![Page 6: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/6.jpg)
MATLAB Programming Basics
Create an array from other arrays:
>> b = [a a]
b
1 2 1 2
3 4 3 4
>> b = [b 1];
ERROR!
Sometimes, this is called a block matrix.
![Page 7: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/7.jpg)
MATLAB Programming Basics
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.
![Page 8: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/8.jpg)
MATLAB Programming Basics
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.
![Page 9: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/9.jpg)
MATLAB Programming Basics
All operators act on arrays:
>> a + a
ans =
2 4
6 8
>> a * a
ans =
6 14
15 25
What would * do for a 3-dimensional array?
![Page 10: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/10.jpg)
MATLAB Programming Basics
All operators act on arrays:
>> a + a
ans =
2 4
6 8
>> a * a
ans =
6 14
15 25
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.
![Page 11: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/11.jpg)
MATLAB Programming Basics
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.
![Page 12: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/12.jpg)
MATLAB Programming Basics
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)
![Page 13: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/13.jpg)
MATLAB Programming Basics
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
1
3
2
4
![Page 14: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/14.jpg)
MATLAB Programming Basics
Reshape reexamined…
Let B be a 3x4x5 array.
That’s 60 elements.
What if we want a 2x3x? array?
2x3x10 = 60, so ? = 10…
![Page 15: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/15.jpg)
MATLAB Programming Basics
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.
![Page 16: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/16.jpg)
MATLAB Programming Basics
Key question: in what order does reshape consider elements?
>>> A
1 2 3 4
5 6 7 8
A looks 2-dimensional
But computer memory is 1-dimensional
Must decide how to lay out values
![Page 17: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/17.jpg)
MATLAB Programming Basics
1 2 3 4 5 6 7 8
1 2 3 4
5 6 7 8
Logical
Physical
Row-major order concatenates the rows
Used by C and Python
![Page 18: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/18.jpg)
MATLAB Programming Basics
1 5 2 6 3 7 4 8
1 2 3 4
5 6 7 8
Logical
Physical
Column-major order concatenates the columns
Used by Fortran and MATLAB
![Page 19: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/19.jpg)
MATLAB Programming Basics
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?
![Page 20: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/20.jpg)
MATLAB Programming Basics
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.
![Page 21: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/21.jpg)
MATLAB Programming Basics
Arrays are dynamically resized:
>> a(3,3) = 1
1 2 0
3 4 0
0 0 1
What would this do:
>> b = 1;
>> b = [b 1];
>> b = [b 1];
![Page 22: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/22.jpg)
MATLAB Programming Basics
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.
![Page 23: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/23.jpg)
MATLAB Programming Basics
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
…
![Page 24: Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See .](https://reader036.fdocuments.us/reader036/viewer/2022062315/5697c00e1a28abf838cca24b/html5/thumbnails/24.jpg)
February 2011
created by
Richard T. Guy
Copyright © Software Carpentry 2011
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.