Introduction to MATLAB Commands for Data Analysis in Geophysics

Slide Note
Embed
Share

Learn how UNIX commands work in MATLAB, differentiate between MATLAB and UNIX commands, utilize MATLAB's help function, explore topics available for help, access help on individual commands, and create constant matrices in MATLAB for data analysis in geophysics.


Uploaded on Oct 08, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. C E R I C E R I- -7 1 04/ C IV L 7 1 04/ C IV L - -8 1 2 6 D ata A nalysis in G eophysics 8 1 2 6 D ata A nalysis in G eophysics C ontinue Introduction to M atlab M iscellaneous stuff and plotting. L ab 5 a, 09 / 1 0/ 1 9

  2. S ome unix commands (pwd, ls, ???) work in matlab (they are actually matlab commands) a=pwd; b=ls; S ome M atlab commands have the same names as U N IX commands, but are not the same B ut cat is a M atlab matrices (not files) M atlab does not pass things it does not understand to the O S to see if they are O S commands. M atlab command that concatenates

  3. help B uilt into matlab help command T o get help on the command command

  4. P roblem when you dont know the name of the command J ust type help >> help HELP topics: Documents/MATLAB - (No table of contents file) matlab/general - General purpose commands. matlab/ops - Operators and special characters. matlab/lang - Programming language constructs. matlab/elmat - Elementary matrices and matrix matlab/randfun - Random matrices and random streams. manipulation. L ists topics of help available

  5. T hen to get contents of topics type help topic >> help elmat Elementary matrices and matrix manipulation. Elementary matrices. zeros - Zeros array. ones - Ones array. eye - Identity matrix. repmat - Replicate and tile array. linspace - Linearly spaced vector. logspace - Logarithmically spaced vector. freqspace - Frequency spacing for frequency response. meshgrid - X and Y arrays for 3-D plots. accumarray - Construct an array with accumulation. : - Regularly spaced vector and index into matrix. Basic array information. size - Size of array.

  6. H elp on individual command >> help zeros ZEROS Zeros array. ZEROS(N) is an N-by-N matrix of zeros. ZEROS(M,N) or ZEROS([M,N]) is an M-by-N matrix of zeros. ZEROS(M,N,P,...) or ZEROS([M N P ...]) is an M-by-N-by-P-by- ... array of zeros. ZEROS(SIZE(A)) is the same size as A and all zeros. ZEROS with no arguments is the scalar 0. ZEROS(M,N,...,CLASSNAME) or ZEROS([M,N,...],CLASSNAME) is an M-by-N-by-... array of zeros of class CLASSNAME. Note: The size inputs M, N, and P... should be nonnegative integers. Negative integers are treated as 0. Example: x = zeros(2,3,'int8'); See also eye, ones. Reference page in Help browser doc zeros

  7. C reate constant matrix T his is something that shows up a lot. >> val=pi val = 3.1416 >> siz=[2 2 2] siz = 2 2 2 >> x=repmat(val,siz) x(:,:,1) = 3.1416 3.1416 3.1416 3.1416 x(:,:,2) = 3.1416 3.1416 3.1416 3.1416 >>

  8. O ther ways (seems more roundabout, showing for completeness) >> xx(prod(siz))=val xx = 0 0 0 0 0 0 0 3.1416 >> xx(:)=xx(end) xx = 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 >> xx=reshape(xx,siz) xx(:,:,1) = 3.1416 3.1416 3.1416 3.1416 xx(:,:,2) = 3.1416 3.1416 3.1416 3.1416 very obscure, weird way but works

  9. A nother way (m, n and o have to be scalar variables, again for completeness) >> m=2 m = 2 >> n=2 n = 2 >> o=2 o = 2 >> x(1:m,1:n,1:o)=val x(:,:,1) = 3.1416 3.1416 3.1416 3.1416 x(:,:,2) = 3.1416 3.1416 3.1416 3.1416 >> x(1:m*n*o)=val A lso works using single dimension addressing

  10. A nother way (actually the most popular, this is T ony's trick!)(val has to be a scalar variable, this syntax populates the array with val) >> x=val(ones(siz)) x(:,:,1) = 3.1416 3.1416 3.1416 3.1416 x(:,:,2) = 3.1416 3.1416 3.1416 3.1416 >> A void using X = val * ones(siz); since it does unnecessary multiplications (versus just storing, above) and only works for classes for which the multiplication operator is defined.

  11. H ow T ony's trick works W hat does this do? H ow does it work? >> x=val([1 1 1; 1 1 1]) W e know what x=val(1) does. W e know what x=val(1,1) does. W e know (or can find out) that x=val(1,2) does not work. W hat about x=val([1])? F rom there easy to generalize a matrix as index. W hat about x=val([1 1])?

  12. H ow T ony's trick works W hat does this do? H ow does it work? >> x=val([1 1 1; 1 1 1]) x = 3.141592653589793 3.141592653589793 3.141592653589793 3.141592653589793 3.141592653589793 3.141592653589793 T ony's trick just replaces the matrix definition [1 1 1; 1 1 1] above with ones(2,3) >> x=val(ones(2,3)) x = 3.141592653589793 3.141592653589793 3.141592653589793 3.141592653589793 3.141592653589793 3.141592653589793

  13. N ames A few things to remember: - C annot use spaces in names of matrices (variables, everything in matlab is a matrix) or filenames cool x = [1 2 3 4 5] - C annot use the dash sign (-) because it represents a subtraction. cool-x = [1 2 3 4 5] - D on t use a period (.) unless you want to create something called a structure. cool.x = [1 2 3 4 5]

  14. A few things to remember: - Y our best option, is to use the underscore ( _ ) if you need to assign a long name to a matrix my_cool_x = [1 2 3 4 5]

  15. O perators review and completeness A rithmetic operators. plus - P lus uplus - U nary plus minus - M inus uminus - U nary minus mtimes - M atrix multiply times - A rray (element by element) multiply ) mpower - M atrix power power - A rray (element by element) power mldivide - B ackslash or left matrix divide mrdivide - S lash or right matrix divide ldivide - L eft array (element by element)divide .\ rdivide - R ight array (element by element) divide ./ kron - K ronecker tensor product + + - - * .* .^ \ ^ / kron

  16. O perators review and completeness R elational operators. eq - E qual ne lt - L ess than gt - G reater than le - Less than or equal ge - G reater than or equal L ogical operators. and - L ogical A N D or - L ogical O R not - L ogical N O T xor - L ogical E X C L U S IV E O R any - T rue if any element is nonzero all - T rue if all elements are nonzero == < > - N ot equal ~= <= >= & | ~ any(A) all(A) xor(A,B)

  17. S ome useful relational operators for whole matrices include the following commands: isequal : tests for equality isempty: tests if an array is empty T hese return 1 if true and 0 if false

  18. C hecking for special elements (NaN, Inf) isnan(a) R eturns 1 for every NaN in array a. isinf(a) R eturns 1 for every Inf in array a. isfinite(a)Returns1 for every finite number (not a (Nan or Inf)) in array a. isreal(a) R eturns 1 for every non-complex number array a.

  19. U sing special elements to your advantage. S ince NaNs propagate through calculations (answer is NaN if there is a NaN somewhere in the calculation), it is sometimes useful to throw NaNs out of operations like taking the mean. (A handy trick to ignore stuff you don t want while you continue calculating.)

  20. E xample of NaNs propagating through calculation (answer is NaN if there is a NaN somewhere in the calculation) >> a=1:4 a = 1 2 3 4 >> b=10:-1:7 b = 10 9 8 7 >> a(2)=NaN a = 1 NaN 3 4 >> a+b ans = 11 NaN 11 11 >>

  21. It is sometimes useful to be able to throw NaNs out of operations like taking the mean. (A handy trick to ignore stuff you don t want while you continue calculating.) S o the function that identifies NaNs can be very useful: >> a a = 1 NaN 3 4 >> ix=find(~isnan(a)) ix = 1 3 4 >> m=mean(a(ix)) m = 2.6667 >> finds all values of a that are not NaNs and averages them (denominator is number of elements averaged, not total number of elements).

  22. T he load function reads binary files containing matrices (generated by earlier M A T L A B sessions), or text files containing numeric data. T he text file should be organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row. N O A L P H A C H A R A C T E R S ! T he text file should be organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row. N O A L P H A C H A R A C T E R S ! >> cat magik.dat 16.0 3.0 5.0 10.0 9.0 6.0 4.0 15.0 >> A=load( magik.dat ) #places matrix in variable A >> load magik.dat #places matrix in variable magik 2.0 11.0 7.0 14.0 13.0 8.0 12.0 1.0

  23. T he save function W rites files containing matrices (from memory). D efault format matlab binary file. >> save >> myfile='my_file.mat >> save(myfile) >> save('my_file.mat','a','b )saves vari a & b to my_file.mat >> save(myfile,'a', '-ascii') saves vari a & b to ascii file "matlab.mat" saves workspace to my_file.mat saves workspace to default name save(FILENAME,VARIABLES) save(FILENAME, , ...,FORMAT) save(FILENAME, ..., '-append') '-mat' Binary MAT-file format (default). '-ascii' 8-digit ASCII format. '-ascii', '-tabs' Tab-delimited 8-digit ASCII format. '-ascii', '-double' 16-digit ASCII format. '-ascii', '-double', '-tabs' Tab-delimited 16-digit ASCII format

  24. M atlab is particularly difficult to use if data files do not fit this format (varying number columns for example). M atlab is also particularly difficult to use for processing character data. W ill return to this later.

  25. W e already know about m-files T ext files with M A T L A B code (instructions). U se M A T L A B E ditor (or any text editor) to create files containing the same statements you would type at the M A T L A B command line. S ave the file with a name that ends in .m % vim magik.m i A = [ 16.0 3.0 2.0 13.0 5.0 10.0 11.0 8.0 9.0 6.0 7.0 12.0 4.0 15.0 14.0 1.0 ]; (esc)wq in matlab, execute the m file magik.m >> magik #places matrix in A don t need the .m

  26. F unctions A re also stored in m-files. T he file name should be the name of the function. F unction stored in file readsac.m function [t,a,p]=readsac(sacfile,varargin) C all with [t,a,p]=readsac(sacfilename)

  27. F lipping vectors or matrices (not the same as the transpose). >> a=[1 2;3 4] a = 1 2 3 4 >> fliplr(a) ans = 2 1 4 3 >> flipud(a) ans = 3 4 1 2 >> a=[1:3;4:6] a = 1 2 3 4 5 6 >> rot90(a) ans = 3 6 2 5 1 4 >> a ans = 1 4 2 5 3 6 >> flipdim(a,1) ans = 4 5 6 1 2 3

  28. H ow to represent nothing E mpty array or string A rray = [] S tring = U seful for defining a name to be used on L H S . S ize and length are zero.

Related