14
Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting arrays Review of array array arithmetic in matlab Functions to manipulate arrays Strings and Cell Arrays Structures More Complex 2D Graphics Basics of handles and get/set

Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Embed Size (px)

Citation preview

Page 1: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Arrays Manipulation and 2D Plots

� Generating arrays

� Array indexing

� Joining, growing and splitting arrays

� Review of array �array arithmetic in matlab

� Functions to manipulate arrays

� Strings and Cell Arrays

� Structures

� More Complex 2D Graphics

� Basics of handles and get/set

Page 2: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Introduction

Today, we are going to spend most of the day discussing matrices (arrays)and their use in matlab. Although this might not seem like the mostfascinating topic, it is crucial that you understand the use and abuse ofmatrices in matlab. If you understand completely this one topic, youbasically understand the bulk of matlab.

Before starting, I would like everyone to change their working directory tothe Windows Desktop. You can either do this with the cd command, or(easier) by clicking on the “ ...” besides the “Current Directory” displayed atthe top of the Matlab Window and selecting the desktop. We will be savingand loading any and all files from the desktop during these classes.

Page 3: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Generating MatricesMatlab makes it easy to generate simple, useful matrices:

» a = ones([5, 1]) % Vector with matrix dimensions

» b = zeros( [2, 3, 4] )

» ones( 5 ) % NOTE: short for ones([5 5]),NOT ones([5,1])

» first = 10; last = 20; number = 5;

» c = linspace( first, last, number ) % See also logspace

» eye( 3 ) % Identity matrix

» rand( [ 2, 3 ] ) % Uniform random nums between 0 and 1.

» randn( size(a) ) % Random nums with a normal distrib.

Note that matlab can quickly generate two types of random numbers. The firstgenerates a number from 0 to 1 with a constant probability for any number betweenthose limits. The second generates a normal or Gaussian distribution of randomnumbers with mean 0 and variance 1. This second type can be used to generatenormal distributions with arbitrary mean and deviation.

You can use the size function to get the dimensions of any array.

� c = size(b);

� [l, w, d] = size(b);

Page 4: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Repeating MatricesChanging the shape or repeating a matrix is also an efficient way to generate usefulmatrices. Consider the following:

� clear all

� a = [ 1 4; 2 5; 3 6 ]

� aa = reshape( 1:6, [3,2] )

� b = [ 1 2 3; 1 2 3; 1 2 3]

� bb = repmat( 1:3, [3, 1] )

Note that a and aa are the same, as are b and bb. reshape takes the matrix givenas the first argument (a vector from 1 to 6 in this case) and changes it'sdimensionsto those given in the second argument. The product of the dimensions must remainthe same.

repmat takes a matrix and reproduces it the number of times given in the secondargument. Play with this function until you are familiar with how it functions.

Finally transposing 2D matrices is easy:

� a = a' ;

Note that the transpose operator can be used at any point in a calculation.

Page 5: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Array IndexingMatlab has a wide variety of ways to access and change parts of a matrix. Theeasiest is with indices:

� clear all

� a = reshape( 1:20, [4,5] )

� a( 1:2, : ) % Gets first two rows

� a( 3:end, : ) % Last three rows

� a( 1:2, 1:3 )

� a( [1 3], [1 3 5] )

� a(2) % Single index works as well � rows, then columns

� a(:) % Turn it into a column vector

end is shorthand for the size of the array in that dimension. : by itself is shorthandfor 1:end.

Arrays can also be accessed via a boolean array of the same dimension. This isuseful for looking at values with certain characteristics.

� b = mod(a,2) == 0 % Set of even values in a

� a(b)

The == operator is NOT for assignment! It tests for equality. Confusing = and ==is one of the most common errors in many programming languages.

Page 6: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Changing ArraysThe indexing explained in the last slide can be used to modify the contents of anarray:

� a = reshape( 1:20, [4,5] ); % Repeat from last slide

� b = mod(a,2) == 0; % Just in case you di dn' t do it

� a(b) = NaN % Get rid of all even numbers

� a

� a( isnan(a) ) = �999

Note that some care needs to be taken when using NaN's. Compare the results of thefollowing commands:

� 15 == 15 % Returns TRUE (1)

� NaN == NaN % Returns FALSE (0) !

� isnan( [1 NaN 3] )

You can also remove entire columns or rows from matrices using the empty matrix.

� a(1,:) = [] % Remove first row

� a(:,3) = [] % Third column

There are also a number of functions useful for testing and indexing matrix contents.See the documentation for all, any and sort.

Page 7: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Joining and Growing ArraysJoining matrices is straightforward in matlab. Follow your nose:

� a = reshape( 1:20, [4,5] );

� c = [a, a] % Join along columns

� [a; a]

� [a, a' ] % DOESNT WORK – must have same no. of rows

� b = ' abcd' ;

� [ b ' ef gh' ] % Strings are ARRAYS of characters

In the last line, I have used the fact that a string is an array to concatenate twostrings.

Matlab automatically grows matrices if a value is assigned to an index beyond thearray size. This is usually useful, but can hide errors.

� a(5,5) = 21 % Fills in with 0' s

� a(:,6) = 1

Growing matrices is computationally intensive. When possible, it is quicker toblock off the appropriate amount of memory (array size) from the start and changevalues as needed.

Page 8: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Array ArithmeticThis is something of a review for everyone:

� a = reshape( 1:20, [4,5] );

� b = a+a

� a * b % DOESNT WORK – bad dimensions

� a * b' % Matrix multiplication

� a ./ b % Element �by �element division

� b .^ a

� c = a ~= a(:,end: �1:1)

� ~c

In matlab, ~ is the logical NOT. ~= stands for not �equal (the opposite of ==).Matrix division (/ and \) also exists and is useful for least �squares fitting. There isalso a kronecker tensor product (kron).

Matlab has all of the typical matrix arithmetic functions. These typically work overcolumns, but this behavior can be modified:

� mean(a)

� min(a)

� sum(a)

� sor t ( a' )

Page 9: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Strings and Cell ArraysMultiple strings can be stored either in character arrays or in cell arrays. Cell arraysare generally easier to use, as will be seen below.

� a = [ ' abcd' ; ' def g' ] % Character arrays with rows

� b = { ' abcd' , ' def g' } % Cell array

� a = [ ' abcd' ; ' def ' ] %DOESNT WORK – string size changes

� b = { ' abcd' , ' def ' }In many ways cell arrays function the same as normal arrays. They can havemultiple dimensions and often be indexed the same as normal arrays. There arehowever some key differences.

� a = { ' a' , 2, ' c ' }

� a([2 3]) % Returns a cell array with one element

� a{[2 3]} % Returns the number 2.

� a{3} = ' Hi '

� a(1) = ' Me' % DOESNT WORK – must be cell array

� a(1) = { ' Me' }When indexed with (), the result is a cell array; when indexed with {}, the result iscalled a list. I won't go into detail, but wrap your brain around the followingexample:

� b = { ' Thi s ' , ' st uf f ' , ' sucks. ' ; ' I t ' , ' i s ' , ' l ame' ;' Thi s ' , ' i sn' ' t ' , ' cool . ' }

� [ b{1,:} ]

� [ b{ eye(3)>0 } ]

Page 10: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

StructuresStructures are the last major data type in matlab. They are useful for putting relateditems together and a particular favorite of mine.

� clear all

� a.name = ' Davi d'

� a.age = 29

� a.height = ' t al l '

� a.location = [ 38.5, 121.7 ]

� a.mom.name = ' El l en'

� a.mom.age = 21

� a

� a.mom

There are also a series of functions for querying structures:

� a = r mf i el d( a, ' hei ght ' )

� i sf i el d( a, ' dad' )

� fieldnames(a)

Structures are most commonly used to organize related, but disparate information.

It is possible to create arrays of structures and cell arrays that contain structures andstructures that contain cell arrays and .....

Page 11: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Plotting 210There are a number of simple ways to make more interesting plots in matlab. Lookat the difference between each of these plots.

� a = 0:0.1:3;

� plotyy( a, exp(a), a, exp( �a) ) % Two independent axes

� semilogy( a, exp(a) )

� loglog(a,exp(a))

You can also put more than one curve on a graph using hold:

� plot(a,exp(a))

� hold on

� plot(a,exp( �a), ' r �. ' )

� hold off

� b = axis % Current axis limits

� b(3) = �1; axis(b) % Change axis limits

There are a number of specialty plots in matlab that are useful at times.

� quiver(a(1:end �1),sin(a(1:end �1)), ...

� diff(a),diff(sin(a)))

� figure

� hist( randn(1,1000), 40)

See also feather, compass, plotmatrix and fill.

Page 12: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Handle GraphicsEssentially all graphics commands in matlab return a handle upon completion. Ahandle is a number that can be used at a later time to change the properties of thegraphics objects associated with that number.

� fh1 = figure

� fh2 = figure % Create a second empty figure window

� figure(fh1) % Make first active

� ph = plot( 1:10, (1:10).^2 )

� figure(fh2)

� [n,x] = hist(randn(1,10000));

� bh = bar ( x, n, ' g' )

� set ( ph, ' l i newi dt h' , 3, ' col or ' , [ 0, 1, 1] ) % RGB color

Using the get command you can find out the current state of the object.

� get(fh1)

� get(bh)

set is used to change the state of the object. By itself, set tells you whatproperties can be changed and their possible values.

� set(fh1)

� set ( f h1, ' posi t i on' , [ 0 0 560 420])

� set ( bh, ' edgecol or ' , [ 1, 0, 1] , ' l i newi dt h' , 4)

See Ch. 31 & the appendix of MM5 for much, much more on handle graphics.

Page 13: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Real �world ExampleToday we are going to plot up some data of sea �surface currents derived from high �

frequency radar installations near Bodega Marine Lab. Links to the necessary filescan be found at the end of this page. Save them to the desktop.

� open coast.txt; open codar.txt

� load coast.txt; load codar.txt

Spatial data is in kilometers from a central point, currents are in cm/s. The goal fortoday is to plot the coast and the nearby current data. First plot the coastline. Makesure to use an “earthy” color. Add a title and axis labels. Use axis equal to fixthe aspect ratio.

Next, you will need to manipulate the current data so that it is useable. Replace all

�999 with NaN's. Perhaps separate the data into good and bad data.

Plot a black point where we have current data and a red point where the current datais missing (NaN).

Next add in the current vectors using quiver. See the help for scaling vectors.

Finally, plot the largest current vector with a different color and a thicker line. Tofind the largest current vector, you will first need to calculate the total velocity in thenormal way and then find the point with the maximum velocity.

Consider using ginput and text to add decoration.

u2 �

v2

Page 14: Arrays Manipulation and 2D Plots - UC Santa Cruzdmk/old-matlab-mini-course/notes... · Arrays Manipulation and 2D Plots Generating arrays Array indexing Joining, growing and splitting

Solution

� load coast.txt

� load currents.txt

� ph = pl ot ( coast ( : , 1) , coast ( : , 2) , ' col or ' , [ 0. 7, 0. 3, 0. 1] )

� axis equal

� currents(currents == � 999) = NaN;

� gc = currents( ~any( i snan( cur r ent s' ) ) , : );� bc = currents( any( i snan( cur r ent s' ) ) , : );

� hold on

� pl ot ( gc( : , 1) , gc( : , 2) , ' k. ' , ' mar ker si ze' , 12)

� pl ot ( bc( : , 1) , bc( : , 2) , ' r . ' , ' mar ker si ze' , 12)

� scale = 0.1;

� quiver(gc(:,1),gc(:,2),scale * gc(:,3),scale * gc(:,4),0)

� tv = sqr t ( sum( gc( : , 3: 4) ' . ^2) ) ;

� top = gc( tv == max(tv), : );

� qh=quiver(top(1),top(2),scale * top(3), scale * top(4), 0);

� set ( qh, ' col or ' , ' m' , ' l i newi dt h' , 3)