






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Matlab bootcamp – Class 2. Written by Kyla Drushka, modified from notes by Darcy Ogden for course SIO113. Aside: scientific notation.
Typology: Summaries
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Written by Kyla Drushka, modified from notes by Darcy Ogden for course SIO
The letter e represents shorthand notation for the exponential 10^. For example >> 1e is equivalent to 1000. >> 9.283e is equivalent to 9283. This is a handy way to enter small numbers: >> 4.229e- 9 is equivalent to 4.229 x 10-^9 Note that Matlab by default only prints out 5 digits (scaled using scientific notation if necessary). You can change the output using the command format. E.g. >> x=1234567. produces x = 1.2346e+ >> format long % shows 15 digits using fixed-point format >> x produces x = 1.234567890123400e+ This doesn't change the precision of the commands performed by Matlab, just the way the output looks.
Save your entire workspace: >> save mystuff or >> save('mystuff') or >> save('mystuff.mat') all produce a file called “mystuff.mat”, which contains all of the variables in your workspace.
>> save('mystuff','x1,'x2','x3','y')* saves variables x1,x2,x3 and all variables beginning with the letter y into the file mystuff.mat You can also include the directory that the file is saved in. >> save('/mydirectory/mystuff') Load .mat files that you have saved: >> load('mystuff') loads all of the variables in mystuff (note, this will overwrite any variables in your current workspace that have the same name) >> load('mystuff','x')* load just variables starting with the letter x We will talk about some other formats for saving on Thursday.
>> d=dir('/mydirectory/') Lists all files and subdirectories in the specified directory, in a structure containing the name, date, size, etc. Selecting subsets of arrays: indexing This is a very powerful and important aspect of Matlab!
element : a single value in an array. E.g. >> g=[9 8 5 2]; % the second element of g is 8. index : the “address” of an element within an array. It is specified using parentheses. >> g(2) % the value of the element at index 2 is 8
>> r=[ 3 2 5 6 1 11] % r is a 1x6 row >> r(3) % the index is 3. This selects the third element of r (which is equal to 5 ) >> v=r(3) % make a new variable, v, and set it equal to the third element of r
Note, >> find(~m) returns the zero indices (~ is like "not", so you are asking to look for all not - zero elements) ans = 6 7
The output of logical expressions is equal to 1 for true and 0 for false, so they can be used as the argument of the find command. For example isnan is a logical operator that returns 1 if its argument is NaN (not a number), and 0 otherwise: **>> v=[1 2 3 nan]
isnan(v)** ans = 0 0 0 1 And since find returns the indices of all non-zero elements of its argument, combining find with the logical operator isnan returns the indices of the elements in v that are NaN: >> find(isnan(v)) ans = 4 That is, the 4th^ element of v is NaN To generate logicals, you can use relational operators such as: <
<=
= == (note, two equals signs are needed for the relational equals) ~= (not equal to) E.g. **>> m=[1 3 5 - 2 - 1 0 0 3]
i=find(m==3)** % returns the index of elements of m that are equal to 3, and stores the index in the variable i: i = 2 8
So far, we have just considered indexing for vectors. For a matrix, you can find the index in (row,column) format by specifying two outputs for find. E.g. >> n=[2 3 4 9 11; 5 - 1 0 - 2 - 2; 6 6 5 5 3 ] n = 2 3 4 9 11 5 - 1 0 - 2 - 2 6 6 5 5 3 >> [rowind colind]=find(n==3) rowind = 1 3 colind = 2 5 That is, n(1,2)=3 and n(3,5)= If you only specify one output when you apply find to a matrix, Matlab returns the “linear index”, which means that it first reshapes your matrix into one long vector (column-wise) and then finds the index in that vector. E.g.: >> i=find(n==3) i = 4 15 Because when you stretch n into a vector, column by column, you get: 2 5 6 3 ç the 4 th element is 3.
Let's set all the missing/bad data to NaN (not a number): >> temperature(badi)=NaN; % now all the elements of temperature corresponding to the index badi are Nan. Matlab does not plot NaN values, so now our figure looks more reasonable: **>> plot(time,temperature)
datetick('x'); ylabel('temperature, degrees C') A note on times/dates with Matlab:** Matlab uses a format for dates called a “serial date number” that is based on the number of days from a reference of Jan 1, year zero. E.g., September 24, 2013 at noon has a serial date number of 735501.5, i.e. we are 735501 days from Jan 1 000 0, and noon represents half a day. You can convert using between yyyy-mm-dd and serial dates using datenum and datestr, e.g.: >> datenum(2001,12,1) % gives the serial date number for December 1, 2001 >> now % gives the serial date number for this moment >> datestr(now) % the date right now, as a string >> datestr(datenum(2001,12,1)) % first convert Dec 1,2001 to a number, then convert it to a string. Now, we can use badi and datestr to get the dates of all the missing data in our temperature time series: >> bad_dates=datestr(time(badi)) bad_dates = 16 - Apr- 1997 16 - May- 1997 16 - Jun- 1997 16 - Oct- 2004 16 - Nov- 2004 16 - Aug- 2007 16 - Aug- 2009 16 - Sep- 2009 16 - Oct- 2009 16 - Nov- 2009
Other logicals used for indexing: You can also use some of these operators, which return logicals, for indexing; these return 1 for true and 0 for false: isnan is a NaN isinf is infinite (Inf or – Inf) isempty is an empty matrix isprime is a prime number e.g. **>> a=[1 3 nan 9 nan 11]
ind=find(isnan(a))** % returns the index of all nan values in a Math with Matlab Element-by-element math It is straightforward to multiply, add, or divide each element of an array by a scalar. (We did this yesterday.) >> x=rand(2,3,4); % a random 3 - dimensional array >> y=2x- 1 ;* % this multiplies each element of x by 2 and subtracts 1 Things get more complicated when you are performing mathematical operations between two arrays. Multiplication, division, and exponential operators have matrix algebra meanings
More useful built-in functions: (taken from Matlab basics notes by Padmanabhan Seshaiyer) Elementary trigonometric functions and their inverses sin, cos, tan, sec, csc, cot, asin, acos, atan, asec, acsc, acot Elementary hyperbolic functions and their inverses sinh, cosh, tanh, sech, csch, coth, asinh, acosh, atanh, asech, acsch, acoth Basic logarithmic and exponentiation functions log, log2, log10, exp, sqrt, pow Basic Statistical functions max, mean, min, median, std, var, sum Basic complex number functions imag, real, i, j, abs, angle, cart2pol Basic data analysis functions fft, ifft, interpn, spline, diff, del2, gradient Basic logical functions and, or, xor, not, any, all, isempty, is* Basic polynomial operations poly, roots, residue, polyfit, polyval,conv Function functions that allows users to manipulate mathematical expressions feval, fminbnd, fzero, quad, ode23, ode45, vectorize, inline, fplot, explot Basic matrix functions zeros, ones, det, trace, norm, eig