Note: The way to "get" this material is to type in the commands I've typed in an verify how they work, and then "play" with them. Also, make use of the "lookfor" and "help" functions, to find out what Matlab does. unix-prompt% matlab -nodesktop < M A T L A B (R) > Copyright 1984-2008 The MathWorks, Inc. Version 7.7.0.471 (R2008b) September 17, 2008 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >> % This is a comment >> % This works a lot like // in C++ >> % It can start anywhere in a line and ends when you type a return >> >> >> >> % There IS a multi-line comment in Matlab: >> >> %{ this is a multiline comment %} >> %{ You can't, however. write on the opening line >> crazy, eh? ??? crazy, eh? | Error: Unexpected MATLAB operator. >> >> >> % In C++: >> % double x = 1; >> >> % In Matlab >> x = 1 x = 1 >> format compact % This makes Matlab provide less space during output % I almost always do this when I use Matlab >> x = 1 x = 1 >> % In Matlab the ; suppresses output, e.g. >> >> x = 2; >> % Want to see what x? Just type it! >> x x = 2 >> x = 1; >> x; % This line does nothing! >> x x = 1 >> y = 2 y = 2 >> % How do you find out what's on the stack (called Workspace in Matlab) >> who % This command lists the variables on the Workspace Your variables are: x y >> what % Shows "M-Files" in the current directory M-files in the current directory /afs/umich.edu/class/engr101/Public/html/Bielajew/classcodes MfileExample floatStuff makeFlag rotcRot90 altLife floodFill plotDemo russianRoulette betterQuadRoots heat private rutherford blifColor heatFast quadRoots simpleLife demoPJandGS heatSlow rotClock90 tryfib demoPcolor ironbar rotCounterClock90 walk1 factorial ironbarGaussSeidel rotc walk2 fastFlag ironbarMSmooth rotcFast walk3 fibTimes ironbarPointJacobi rotcFliplr fibonacci life rotcMalloc >> why % Some silly person at Matlab Inc. programmed this! Pete wanted it that way. >> >> where % Does not exist in Matlab ??? Undefined function or variable 'where'. >> % This is what happens when something is not defined >> how % Does not exist in Matlab ??? Undefined function or variable 'how'. >> who Your variables are: x y >> whos % This is a longer form of "who" Name Size Bytes Class x 1x1 8 double array y 1x1 8 double array Grand total is 2 elements using 16 bytes >> % Note that everything is considered to be a two-dimensional array >> clear % takes variables off the stack >> who % Nothing there! >> clear all % Clears just about everything >> x = 1, y = 2 % Note that a comma (,) can be used put more than one >> % statement on a line WITH output. You can also put >> % multiple statements on a line with semicolons (;), but >> % output will be suppressed. x = 1 y = 2 >> whos Name Size Bytes Class x 1x1 8 double array y 1x1 8 double array Grand total is 2 elements using 16 bytes >> clc % Clears the command window and homes the cursor >> help % General help command HELP topics toolbox/NAG - NAG Toolbox matlab/general - General purpose commands. matlab/ops - Operators and special characters. matlab/lang - Programming language constructs. matlab/elmat - Elementary matrices and matrix manipulation. matlab/randfun - Random matrices and random streams. matlab/elfun - Elementary math functions. matlab/specfun - Specialized math functions. matlab/matfun - Matrix functions - numerical linear algebra. matlab/datafun - Data analysis and Fourier transforms. matlab/polyfun - Interpolation and polynomials. matlab/funfun - Function functions and ODE solvers. matlab/sparfun - Sparse matrices. matlab/scribe - Annotation and Plot Editing. matlab/graph2d - Two dimensional graphs. matlab/graph3d - Three dimensional graphs. matlab/specgraph - Specialized graphs. matlab/graphics - Handle Graphics. matlab/uitools - Graphical User Interface Tools. matlab/strfun - Character strings. matlab/imagesci - Image and scientific data input/output. matlab/iofun - File input and output. matlab/audiovideo - Audio and Video support. matlab/timefun - Time and dates. matlab/datatypes - Data types and structures. matlab/verctrl - Version control. matlab/codetools - Commands for creating and debugging code. matlab/helptools - Help commands. matlab/demos - Examples and demonstrations. matlab/timeseries - Time series data visualization and exploration. matlab/hds - (No table of contents file) matlab/guide - Graphical User Interface Tools. matlab/plottools - Graphical User Interface Tools. toolbox/local - General preferences and configuration information. shared/controllib - Control Library shared/dastudio - (No table of contents file) matlab/datamanager - (No table of contents file) simulink/simulink - Simulink simulink/blocks - Simulink block library. simulink/components - Simulink components. simulink/fixedandfloat - Simulink Fixed Point utilities. fixedandfloat/fxpdemos - Simulink Fixed Point Demos fixedandfloat/obsolete - (No table of contents file) simulink/simdemos - Simulink demonstrations and examples. simdemos/aerospace - Simulink: Aerospace model demonstrations and samples. simdemos/automotive - Simulink: Automotive model demonstrations and samples. simdemos/simfeatures - Simulink: Feature demonstrations and samples. simdemos/simgeneral - Simulink: General model demonstrations and samples. simulink/dee - Differential Equation Editor dastudio/depviewer - (No table of contents file) stateflow/stateflow - Stateflow rtw/rtw - Real-Time Workshop shared/sigbldr - (No table of contents file) simulink/modeladvisor - (No table of contents file) simulink/MPlayIO - (No table of contents file) shared/hdlshared - (No table of contents file) rtw/accel - (No table of contents file) rtw/rtwdemos - (No table of contents file) rtwdemos/rsimdemos - (No table of contents file) asap2/asap2 - (No table of contents file) can/blocks - (No table of contents file) configuration/resource - (No table of contents file) common/tgtcommon - (No table of contents file) targets/connectivity - (No table of contents file) targets/pil - (No table of contents file) stateflow/sfdemos - Stateflow demonstrations and samples. stateflow/coder - Stateflow Coder aeroblks/aeroblks - Aerospace Blockset aeroblks/aerodemos - Aerospace Blockset demonstrations and examples. aero/aero - Aerospace Toolbox aero/astdemos - (No table of contents file) shared/mapgeodesy - Geometric Geodesy Functions AUTOSAR/AUTOSAR - (No table of contents file) commblks/commblks - Communications Blockset commblks/commmasks - Communications Blockset library block mask helper functions. commblks/commmex - Communications Blockset S-function MEX-files. commblks/commblksdemos - Communications Blockset Demos. commblksobsolete/v3 - (No table of contents file) commblksobsolete/v2p5 - (No table of contents file) commblksobsolete/v2 - (No table of contents file) comm/comm - Communications Toolbox comm/commdemos - Communications Toolbox Demos. commdemos/commdocdemos - Communications Toolbox Documentation Examples. comm/commobsolete - Archived MATLAB Files from Communications Toolbox Version 1.5. toolbox/compiler - MATLAB Compiler control/control - Control System Toolbox control/ctrlguis - Control System Toolbox -- Visualization and plot manipulation. control/ctrlobsolete - Control System Toolbox -- obsolete commands. control/ctrlutil - Control System Toolbox -- Utilities and MEX files. control/ctrldemos - Control System Toolbox -- Demos. shared/slcontrollib - Simulink Control Design Library curvefit/curvefit - Curve Fitting Toolbox curvefit/cftoolgui - (No table of contents file) shared/optimlib - Optimization Toolbox Library data_manager/data_manager - (No table of contents file) des/desblks - SimEvents des/desmasks - (No table of contents file) des/desmex - (No table of contents file) des/desdemos - SimEvents Demos. toolbox/distcomp - Parallel Computing Toolbox distcomp/user - (No table of contents file) distcomp/mpi - (No table of contents file) distcomp/pctdemos - (No table of contents file) distcomp/parallel - Parallel Algorithms parallel/datafun - (No table of contents file) parallel/datatypes - (No table of contents file) parallel/elfun - (No table of contents file) parallel/elmat - (No table of contents file) parallel/lapack - (No table of contents file) parallel/matfun - (No table of contents file) parallel/ops - (No table of contents file) parallel/sparfun - (No table of contents file) parallel/specfun - (No table of contents file) parallel/util - (No table of contents file) distcomp/lang - Parallel computing programming language constructs. dspblks/dspblks - Signal Processing Blockset dspblks/dspmasks - Signal Processing Blockset mask helper functions. dspblks/dspmex - DSP Blockset S-Function MEX-files. dspblks/dspdemos - Signal Processing Blockset demonstrations and examples. shared/filterdesignlib - (No table of contents file) targets/ecoder - Real-Time Workshop Embedded Coder ecoder/ecoderdemos - (No table of contents file) mpt/mpt - Module Packaging Features mpt/user_specific - (No table of contents file) emlcoder/emlcoder - Embedded MATLAB Coder. eml/eml - (No table of contents file) filterdesign/filterdesign - Filter Design Toolbox filterdesign/quantization - (No table of contents file) filterdesign/filtdesdemos - (No table of contents file) finance/finance - Financial Toolbox finance/calendar - Financial Toolbox calendar functions. finance/findemos - Financial Toolbox demonstration functions. finance/finsupport - (No table of contents file) finance/ftseries - Financial Toolbox Times Series Functions. finance/ftsdemos - (No table of contents file) finance/ftstutorials - (No table of contents file) fixedpoint/fixedpoint - Fixed-Point Toolbox fixedpoint/fidemos - (No table of contents file) fixedpoint/fixedpointtool - (No table of contents file) fuzzy/fuzzy - Fuzzy Logic Toolbox fuzzy/fuzdemos - Fuzzy Logic Toolbox Demos. ident/ident - System Identification Toolbox ident/nlident - Nonlinear System Identification features. Type "help ident" for more info. ident/idobsolete - (No table of contents file) ident/idguis - (No table of contents file) ident/idutils - (No table of contents file) ident/iddemos - (No table of contents file) iddemos/examples - (No table of contents file) images/colorspaces - Image Processing Toolbox --- colorspaces images/images - Image Processing Toolbox images/imdemos - Image Processing Toolbox --- demos and sample images images/imuitools - Image Processing Toolbox --- imuitools images/iptformats - Image Processing Toolbox --- File Formats images/iptutils - Image Processing Toolbox --- utilities shared/imageslib - Image Processing Toolbox Library shared/spcuilib - (No table of contents file) instrument/instrument - Instrument Control Toolbox instrument/instrumentdemos - (No table of contents file) instrumentblks/instrumentblks - (No table of contents file) instrumentblks/instrumentmex - (No table of contents file) instrumentblks/instrumentmasks - (No table of contents file) shared/testmeaslib - (No table of contents file) map/map - Mapping Toolbox map/mapdemos - Mapping Toolbox Demos and Sample Data Sets. map/mapdisp - (No table of contents file) map/mapformats - (No table of contents file) map/mapproj - (No table of contents file) map/maputils - (No table of contents file) mech/mech - SimMechanics mech/mechdemos - SimMechanics Demos. mech/importer - (No table of contents file) slvnv/simcoverage - Simulink Model Coverage Tool network_engine/network_engine - (No table of contents file) network_engine/ne_sli - (No table of contents file) toolbox/nnet - Neural Network Toolbox nnet/nncontrol - Neural Network Toolbox Control System Functions. nnet/nndemos - Neural Network Demonstrations. nnet/nnet - (No table of contents file) nnet/nnanalyze - (No table of contents file) nnet/nncustom - (No table of contents file) nnet/nndistance - (No table of contents file) nnet/nnformat - (No table of contents file) nnet/nninit - (No table of contents file) nnet/nnlearn - (No table of contents file) nnet/nnnetinput - (No table of contents file) nnet/nnnetwork - (No table of contents file) nnet/nnperformance - (No table of contents file) nnet/nnplot - (No table of contents file) nnet/nnprocess - (No table of contents file) nnet/nnsearch - (No table of contents file) nnet/nntopology - (No table of contents file) nnet/nntrain - (No table of contents file) nnet/nntransfer - (No table of contents file) nnet/nnweight - (No table of contents file) nnguis/nftool - (No table of contents file) nnguis/nntool - (No table of contents file) nnet/nnobsolete - Neural Network Toolbox nnet/nnutils - Neural Network Toolbox nnguis/nnguiutils - (No table of contents file) nndemos/nndatasets - (No table of contents file) nnguis/nntraintool - (No table of contents file) optim/optim - Optimization Toolbox optim/optimdemos - Demonstrations of large-scale methods. toolbox/pde - Partial Differential Equation Toolbox pm_sli/pm_sli - (No table of contents file) pm_util/pm_util - (No table of contents file) powersys/powersys - SimPowerSystems powersys/powerdemo - SimPowerSystems Demos drives/drivesdemo - (No table of contents file) DR/DRdemo - (No table of contents file) rfblks/rfblks - RF Blockset rfblks/rfblksmasks - RF Blockset mask helper functions. rfblks/rfblksmex - RF Blockset S-functions. rfblks/rfblksdemos - RF Blockset Demos. rf/rf - RF Toolbox rf/rfdemos - RF Toolbox Demos. rf/rftool - RF Tool (GUI) robust/robust - Robust Control Toolbox robust/rctlmi - Robust Control Toolbox - LMI Solvers. robust/rctutil - (No table of contents file) robust/rctdemos - (No table of contents file) rctobsolete/lmi - (No table of contents file) mutools/commands - (No table of contents file) mutools/subs - Utilities for obsolete MUTOOLS commands. sh/sh - SimHydraulics signal/signal - Signal Processing Toolbox signal/sigtools - (No table of contents file) signal/sptoolgui - (No table of contents file) signal/sigdemos - (No table of contents file) simscape/simscape - Simscape slcontrol/slcontrol - Simulink Control Design slcontrol/slctrlguis - (No table of contents file) slcontrol/slctrlutil - (No table of contents file) slcontrol/slctrldemos - (No table of contents file) shared/sldv - (No table of contents file) toolbox/splines - Spline Toolbox toolbox/stats - Statistics Toolbox shared/statslib - Statistics Toolbox Library toolbox/symbolic - Symbolic Math Toolbox vipblks/vipblks - Video and Image Processing Blockset vipblks/vipmasks - (No table of contents file) vipblks/vipmex - (No table of contents file) vipblks/vipdemos - Video and Image Processing Blockset demonstrations and examples. vr/vr - Virtual Reality Toolbox vr/vrdemos - Virtual Reality Toolbox examples. wavelet/wavelet - Wavelet Toolbox wavelet/wmultisig1d - (No table of contents file) wavelet/wavedemo - (No table of contents file) wavelet/compression - (No table of contents file) bioinfo/bioinfo - Bioinformatics Toolbox bioinfo/biolearning - Bioinformatics Toolbox -- Statistical Learning functions. bioinfo/microarray - Bioinformatics Toolbox -- Microarray support functions. bioinfo/mass_spec - Bioinformatics Toolbox -- Mass spectrometry data analysis functions. bioinfo/proteins - Bioinformatics Toolbox -- Protein analysis tools. bioinfo/biomatrices - Bioinformatics Toolbox -- Sequence similarity scoring matrices. bioinfo/biodemos - Bioinformatics Toolbox -- Tutorials, demos and examples. bioinfo/graphtheory - Bioinformatics Toolbox -- Graph Theory functions. database/database - Database Toolbox database/dbdemos - Database Toolbox Demonstration Functions. database/vqb - Visual Query Builder functions. datafeed/datafeed - Datafeed Toolbox datafeed/dfgui - Datafeed Toolbox Graphical User Interface discovery/discovery - EDA Simulator Link DS discovery/discoverydemos - (No table of contents file) shared/hdllink - (No table of contents file) distcomp/worker - MATLAB Distributed Computing Server drive/drive - SimDriveline drive/drivedemos - (No table of contents file) econ/econ - Econometrics Toolbox econ/econdemos - (No table of contents file) elec/elec - SimElectronics elec/elecdemos - (No table of contents file) finderiv/finderiv - Financial Derivatives Toolbox finfixed/finfixed - Fixed-Income Toolbox finfixed/finfixdemos - (No table of contents file) toolbox/fixpoint - Simulink Fixed Point gads/gads - Genetic Algorithm and Direct Search Toolbox gads/gadsdemos - Genetic Algorithm and Direct Search Toolbox Demos mpc/mpc - Model Predictive Control Toolbox mpc/mpcdemos - (No table of contents file) mpc/mpcguis - (No table of contents file) mpc/mpcobsolete - Contents of the previous (obsolete) version of MPC Toolbox mpc/mpcutils - (No table of contents file) slvnv/reqmgt - Requirements Management Interface. slvnv/rmidemos - (No table of contents file) rptgenext/rptgenext - Simulink Report Generator rptgenext/rptgenextv1 - (No table of contents file) rptgen/rptgen - MATLAB Report Generator rptgen/rptgendemos - (No table of contents file) rptgen/rptgenv1 - (No table of contents file) simbio/simbio - SimBiology simbio/simbiodemos - SimBiology Demos slestim/slestdemos - Simulink Parameter Estimation Demos slestim/slestguis - (No table of contents file) slestim/slestim - Simulink Parameter Estimation slestim/slestmex - Simulink Parameter Estimation S-Function MEX-files. slestim/slestutil - (No table of contents file) sloptim/sloptim - Simulink Response Optimization sloptim/sloptguis - (No table of contents file) sloptim/sloptdemos - Simulink Response Optimization Demos. sloptim/sloptobsolete - (No table of contents file) slvnv/slvnv - Simulink Verification and Validation slvnv/simcovdemos - (No table of contents file) systemtest/systemtest - SystemTest systemtest/systemtestdemos - SystemTest demonstrations and examples. systemtest/viewer - (No table of contents file) shared/imaqlib - Image Acquisition Toolbox Library >> help help % Helps on use of the help command HELP Display help text in Command Window. HELP, by itself, lists all primary help topics. Each primary topic corresponds to a directory name on the MATLABPATH. HELP / lists a description of all operators and special characters. HELP FUN displays a description of and syntax for the function FUN. When FUN is in multiple directories on the MATLAB path, HELP displays information about the first FUN found on the path and lists PATHNAME/FUN for other (overloaded) FUNs. HELP PATHNAME/FUN displays help for the function FUN in the PATHNAME directory. Use this syntax to get help for overloaded functions. HELP DIR displays a brief description of each function in the MATLAB directory DIR. DIR can be a relative partial pathname (see HELP PARTIALPATH). When there is also a function called DIR, help for both the directory and the function are provided. HELP CLASSNAME.METHODNAME displays help for the method METHODNAME of the fully qualified class CLASSNAME. To determine CLASSNAME for METHODNAME, use CLASS(OBJ), where METHODNAME is of the same class as the object OBJ. HELP CLASSNAME displays help for the fully qualified class CLASSNAME. T = HELP('TOPIC') returns the help text for TOPIC as a string, with each line separated by /n. TOPIC is any allowable argument for HELP. REMARKS: 1. Use MORE ON before running HELP to pause HELP output after a screenful of text displays. 2. In the help syntax, function names are capitalized to make them stand out. In practice, always type function names in lowercase. For Java functions that are shown with mixed case (for example, javaObject) type the mixed case as shown. 3. Use DOC FUN to display help about the function in the Help browser, which might provide additional information, such as graphics and examples. 4. Use DOC HELP for information about creating help for your own M-files. 5. Use HELPBROWSER to access online documentation in the Help browser. Use the Help browser Index or Search tabs to find more information about TOPIC or other terms. EXAMPLES: help close - displays help for the CLOSE function. help database/close - displays help for the CLOSE function in the Database Toolbox. help database - lists all functions in the Database Toolbox and displays help for the DATABASE function. help general - lists all functions in the directory MATLAB/GENERAL. help embedded.fi - displays help for the EMBEDDED.FI class in the Fixed-Point Toolbox. help embedded.fi.lsb displays help for the LSB method of the EMBEDDED.FI class in the Fixed-Point Toolbox. t = help('close') - gets help for the function CLOSE and stores it as a string in t. See also DOC, DOCSEARCH, HELPBROWSER, HELPWIN, LOOKFOR, MATLABPATH, MORE, PARTIALPATH, WHICH, WHOS, CLASS. Reference page in Help browser doc help >> lookfor sine % Look for commands that have the string "sine" in their >> % description c06hd - Discrete quarter-wave cosine transform c06ra - Discrete sine transform (easy-to-use) c06hb - Discrete cosine transform s13ac - Cosine integral Ci((x)) c06hc - Discrete quarter-wave sine transform s13ad - Sine integral Si((x)) c06ha - Discrete sine transform c06rd - Discrete quarter-wave cosine transform (easy-to-use) c06rb - Discrete cosine transform (easy-to-use) c06rc - Discrete quarter-wave sine transform (easy-to-use) cosh - Hyperbolic cosine. asin - Inverse sine, result in radians. cos - Cosine of argument in radians. sinh - Hyperbolic sine. acosh - Inverse hyperbolic cosine. acos - Inverse cosine, result in radians. sind - Sine of argument in degrees. acosd - Inverse cosine, result in degrees. cosd - Cosine of argument in degrees. sin - Sine of argument in radians. asind - Inverse sine, result in degrees. asinh - Inverse hyperbolic sine. xfourier - Square Wave from Sine Waves slsincos - This is a private mask helper file for sine and cosine blocks in ssinthd - calculate total harmonic distortion for a sampled sine wave sldemo_wavethd_script - Digital Waveform Generation: Approximating a Sine Wave dcmecef2ned - Convert geodetic latitude and longitude to direction cosine matrix. quat2dcm - Convert quaternion to direction cosine matrix. dcmbody2wind - Convert angle of attack and sideslip angle to direction cosine matrix. dcm2latlon - Convert direction cosine matrix to geodetic latitude and longitude. dcm2angle - Create rotation angles from direction cosine matrix. dcm2alphabeta - Convert direction cosine matrix to angle of attack and sideslip angle. dcm2quat - Convert direction cosine matrix to quaternion. angle2dcm - Create direction cosine matrix from rotation angles. commblkrcfilttx - Raised cosine Transmit FIR filter blocks helper function. commblkrcfiltrx - Raised cosine Receiver FIR filter blocks helper function. rcosfir - Design a raised cosine FIR filter. rcosflt - Filter the input signal using a raised cosine filter. rcosine - Design raised cosine filter. rcosiir - Design a raised cosine IIR filter. rcosdemo - Raised Cosine Filtering commdoc_rrc - Pulse Shaping Using a Raised Cosine Filter dspblksine - Signal Processing Blockset Sine Wave block helper function. isbusday - True for dates that are business days. lbusdate - Last business date of month. busdays - generates a vector of business days in serial date format. busdate - Next or previous business day. fbusdate - First business date of month. dct2 - 2-D discrete cosine transform. dctmtx - Discrete cosine transform matrix. idct2 - 2-D inverse discrete cosine transform. modsine - Tissot Modified Sinusoidal Pseudocylindrical Projection goode - Goode Homolosine Pseudocylindrical Projection mech_car_raised_cosine - Helper function to create a raised cosine pulse at time 'T' idst - Inverse discrete sine transform. dst - Discrete sine transform. dct - Discrete cosine transform. idct - Inverse discrete cosine transform. rc2is - Convert reflection coefficients to inverse sine parameters. chirp - Swept-frequency cosine generator. is2rc - Convert inverse sine parameters to reflection coefficients. firrcos - Raised Cosine FIR Filter design. tffunc - time and frequency domain versions of a cosine modulated Gaussian pulse. sinint - Sine integral function. cosint - Cosine integral function. % Note: You can use control-C to stop something Matlab is doing >> help sin % More info on the sin function SIN Sine of argument in radians. SIN(X) is the sine of the elements of X. See also ASIN, SIND. Overloaded methods: codistributed/sin distributed/sin sym/sin Reference page in Help browser doc sin >> >> x x = 1 >> sin(x) % This will capture the result in a special variable called "ans" ans = 0.8415 >> t = ans; % Make a new variable called t and assign ans's value to it >> whos Name Size Bytes Class ans 1x1 8 double array t 1x1 8 double array x 1x1 8 double array y 1x1 8 double array Grand total is 4 elements using 32 bytes >> clear t % Erase t from the stack >> whos Name Size Bytes Class ans 1x1 8 double array x 1x1 8 double array y 1x1 8 double array Grand total is 3 elements using 24 bytes >> z = sin(x) % Note, x is in radians z = 0.8415 >> whos Name Size Bytes Class ans 1x1 8 double array x 1x1 8 double array y 1x1 8 double array z 1x1 8 double array Grand total is 4 elements using 32 bytes >> clear >> x x = 1 >> disp(x) % Another way to display something 1 >> z = x + y z = 3 >> x = ones(2) % Make a 2x2 array of ones x = 1 1 1 1 >> y = 2*ones(2) % Make a 2x2 array of ones, multiply by 2 and call it y y = 2 2 2 2 >> z = x + y % Note that the addition is a "pointwise" operator z = 3 3 3 3 >> whos Name Size Bytes Class Attributes x 2x2 32 double y 2x2 32 double z 2x2 32 double >> x = 1 % The previous x gets ``clobbered" x = 1 >> y = 2 % So does te previous y y = 2 >> whos Name Size Bytes Class ans 1x1 8 double array x 1x1 8 double array y 1x1 8 double array z 2x2 32 double array Grand total is 7 elements using 56 bytes >> clear z >> whos Name Size Bytes Class x 1x1 8 double array y 1x1 8 double array Grand total is 2 elements using 16 bytes >> z = x*y % Multiplication of scalars z = 2 >> x = ones(2) x = 1 1 1 1 >> y = 2*ones(2) y = 2 2 2 2 >> z = x*y % Matrix multiplication! Be careful! z = 4 4 4 4 >> % What a matrix multiplication does is: >> % for (int i = 0; i < ROWS; i = i + 1) >> % { for (int j = 0; j < COLS; j = j + 1) >> % { z[i][j] = 0 >> % for (int k = 0; k < N; k = k + 1) >> % z[i][j] = z[i][j] + x[i][k]*y[k][j]; >> % } >> % } >> z = x.*y % Pointwise multiplication z = 2 2 2 2 >> % What a point-wise matrix multiplication does is: >> % for (int i = 0; i < ROWS; i = i + 1) >> % for (int j = 0; j < COLS; j = j + 1) >> % z[i][j] = x[i][j]*y[i][j]; >> % >> % IN THIS COURSE: >> % Whenever we mean to say * or / we'll use .* or ./ >> >> a = [1,2;3,4] % Explicit creation of an array a = 1 2 3 4 >> % It looks more C++-like if we do the following >> a = [ ... 1,2; ... 3,4 ... ] % Explicit creation of an array >> % Note the use of ..., used to continue lines. >> % This is required since a line in Matlab is ended >> % when you type a return >> a' % ' is the transpose operator (about the main diagonal) ans = 1 3 2 4 >> % The transpose operator "flips" the matrix about the main diagonal >> >> v = linspace(1,10,6) % Make vector v a 6-element row vector evenly spaced >> % between 1 and 10 v = 1.0000 2.8000 4.6000 6.4000 8.2000 10.0000 >> whos Name Size Bytes Class a 2x2 32 double array ans 2x2 32 double array v 1x6 48 double array x 2x2 32 double array y 2x2 32 double array z 2x2 32 double array Grand total is 26 elements using 208 bytes >> v = v' % The transpose of a row vector is a column vector v = 1.0000 2.8000 4.6000 6.4000 8.2000 10.0000 >> whos Name Size Bytes Class a 2x2 32 double array ans 2x2 32 double array v 6x1 48 double array x 2x2 32 double array y 2x2 32 double array z 2x2 32 double array Grand total is 26 elements using 208 bytes >> q = 5 q = 5 >> q' % The transpose of a scalar, is itself q = 5 >> a = [1,2;3] % Error because explicit declaration was incomplete ??? Error using ==> vertcat All rows in the bracketed expression must have the same number of columns. >> a % Previous version of a was not clobbered a = 1 2 3 4 >> clear % Clear the Workspace >> whos % Verify that nothing is there >> x = 2 x = 2 >> y = 3 y = 3 >> % In C++ we could have used pow(x.y) >> z = x^y % ^ is the exponentiation operator z = 8 >> b = 2*ones(2) b = 2 2 2 2 >> b^3 % Matrix operation! Careful! ans = 32 32 32 32 >> b*b*b % Same as b^3 ans = 32 32 32 32 >> b.^3 % Pointwise exponentiation ans = 8 8 8 8 >> % demonstrating the use of if structures >> x = 137 x = 137 >> %&& MATLAB %%% %%% C++ %%% >> if (x <= 0) % if (x <= 0) x = -x; % { x = -x;} elseif (x > 100) % else if (x > 100) x = x - 100; % { x = x - 100;} else % else x = 0; % { x = 0; end % } >> x % cout << x; x = 37 >> % In Matlab we have | instead of || as in C++ >> % In Matlab we have & instead of && as in C++ >> % In Matlab we have ~ instead of ! as in C++ >> % In Matlab we have ~= instead of != as in C++ >> help relop Relational operators. < > Relational operators. The six relational operators are <, <=, >, >=, ==, and ~=. A < B does element by element comparisons between A and B and returns a matrix of the same size with elements set to logical 1 (TRUE) where the relation is true and elements set to logical 0 (FALSE) where it is not. A and B must have the same dimensions (or one can be a scalar). & Element-wise Logical AND. A & B is a matrix whose elements are logical 1 (TRUE) where both A and B have non-zero elements, and logical 0 (FALSE) where either has a zero element. A and B must have the same dimensions (or one can be a scalar). && Short-Circuit Logical AND. A && B is a scalar value that is the logical AND of scalar A and B. This is a "short-circuit" operation in that MATLAB evaluates B only if the result is not fully determined by A. For example, if A equals 0, then the entire expression evaluates to logical 0 (FALSE), regard- less of the value of B. Under these circumstances, there is no need to evaluate B because the result is already known. | Element-wise Logical OR. A | B is a matrix whose elements are logical 1 (TRUE) where either A or B has a non-zero element, and logical 0 (FALSE) where both have zero elements. A and B must have the same dimensions (or one can be a scalar). || Short-Circuit Logical AND. A || B is a scalar value that is the logical OR of scalar A and B. This is a "short-circuit" operation in that MATLAB evaluates B only if the result is not fully determined by A. For example, if A equals 1, then the entire expression evaluates to logical 1 (TRUE), regard- less of the value of B. Under these circumstances, there is no need to evaluate B because the result is already known. ~ Logical complement (NOT). ~A is a matrix whose elements are logical 1 (TRUE) where A has zero elements, and logical 0 (FALSE) where A has non-zero elements. xor Exclusive OR. xor(A,B) is logical 1 (TRUE) where either A or B, but not both, is non-zero. See XOR. >> help ops Operators and special characters. Arithmetic operators. plus - Plus + uplus - Unary plus + minus - Minus - uminus - Unary minus - mtimes - Matrix multiply * times - Array multiply .* mpower - Matrix power ^ power - Array power .^ mldivide - Backslash or left matrix divide \ mrdivide - Slash or right matrix divide / ldivide - Left array divide .\ rdivide - Right array divide ./ kron - Kronecker tensor product kron Relational operators. eq - Equal == ne - Not equal ~= lt - Less than < gt - Greater than > le - Less than or equal <= ge - Greater than or equal >= Logical operators. Short-circuit logical AND && Short-circuit logical OR || and - Element-wise logical AND & or - Element-wise logical OR | not - Logical NOT ~ xor - Logical EXCLUSIVE OR any - True if any element of vector is nonzero all - True if all elements of vector are nonzero Special characters. colon - Colon : paren - Parentheses and subscripting ( ) paren - Brackets [ ] paren - Braces and subscripting { } punct - Function handle creation @ punct - Decimal point . punct - Structure field access . punct - Parent directory .. punct - Continuation ... punct - Separator , punct - Semicolon ; punct - Comment % punct - Invoke operating system command ! punct - Assignment = punct - Quote ' transpose - Transpose .' ctranspose - Complex conjugate transpose ' horzcat - Horizontal concatenation [,] vertcat - Vertical concatenation [;] subsasgn - Subscripted assignment ( ),{ },. subsref - Subscripted reference ( ),{ },. subsindex - Subscript index Bitwise operators. bitand - Bit-wise AND. bitcmp - Complement bits. bitor - Bit-wise OR. bitmax - Maximum floating point integer. bitxor - Bit-wise XOR. bitset - Set bit. bitget - Get bit. bitshift - Bit-wise shift. Set operators. union - Set union. unique - Set unique. intersect - Set intersection. setdiff - Set difference. setxor - Set exclusive-or. ismember - True for set member. See also ARITH, RELOP, SLASH, FUNCTION_HANDLE. >> x = 0 x = 0 >> y = 1 y = 1 >> >> x & y ans = 0 >> >> x | y ans = 1 >> >> x < y ans = 1 >> >> x > y ans = 0 >> >> x <= y ans = 1 >> >> x >= y ans = 0 >> >> x == y ans = 0 >> >> x = y % Careful! This is NOT a logical expression x = 1 >> >> % list generation by the symbol : >> 1:10 ans = 1 2 3 4 5 6 7 8 9 10 >> >> % startingPoint:stride:maxPossibleValue >> 1:2:10 ans = 1 3 5 7 9 >> 2:3:37 ans = 2 5 8 11 14 17 20 23 26 29 32 35 >> rv = 2:3:37 rv = 2 5 8 11 14 17 20 23 26 29 32 35 >> rv rv = 2 5 8 11 14 17 20 23 26 29 32 35 >> whos Name Size Bytes Class ans 1x12 96 double array rv 1x12 96 double array x 1x1 8 double array y 1x1 8 double array Grand total is 26 elements using 208 bytes >> rv rv = 2 5 8 11 14 17 20 23 26 29 32 35 >> cv = rv' cv = 2 5 8 11 14 17 20 23 26 29 32 35 >> clear >> whos >> linspace(0,pi,18) ans = Columns 1 through 7 0 0.1848 0.3696 0.5544 0.7392 0.9240 1.1088 Columns 8 through 14 1.2936 1.4784 1.6632 1.8480 2.0328 2.2176 2.4024 Columns 15 through 18 2.5872 2.7720 2.9568 3.1416 >> x = linspace(0,pi,18) x = Columns 1 through 7 0 0.1848 0.3696 0.5544 0.7392 0.9240 1.1088 Columns 8 through 14 1.2936 1.4784 1.6632 1.8480 2.0328 2.2176 2.4024 Columns 15 through 18 2.5872 2.7720 2.9568 3.1416 >> x x = Columns 1 through 7 0 0.1848 0.3696 0.5544 0.7392 0.9240 1.1088 Columns 8 through 14 1.2936 1.4784 1.6632 1.8480 2.0328 2.2176 2.4024 Columns 15 through 18 2.5872 2.7720 2.9568 3.1416 >> y = 0:pi/17:pi y = Columns 1 through 7 0 0.1848 0.3696 0.5544 0.7392 0.9240 1.1088 Columns 8 through 14 1.2936 1.4784 1.6632 1.8480 2.0328 2.2176 2.4024 Columns 15 through 18 2.5872 2.7720 2.9568 3.1416 >> x - y ans = 1.0e-15 * Columns 1 through 7 0 0 0 0 0 0 0 Columns 8 through 14 -0.2220 0 0 0.2220 -0.4441 0 0.4441 Columns 15 through 18 0 -0.4441 0 0 >> help linspace LINSPACE Linearly spaced vector. LINSPACE(X1, X2) generates a row vector of 100 linearly equally spaced points between X1 and X2. LINSPACE(X1, X2, N) generates N points between X1 and X2. For N < 2, LINSPACE returns X2. See also LOGSPACE, :. Reference page in Help browser doc linspace >> clear >> s = 0; i = 0; while(i < 10) i = i +1; s = s + i; end >> s s = 55 >> i i = 10 >> clear all >> s = 0; for i = 1:10 s = s + i; end >> s s = 55 >> s = 0; >> for i = 1:10 for j = 1:20 s = s + i + j; end end >> s s = 3200 >> >> %{ Important note: Recall in C++ the for structure was very flexible for(Define and assign; logical expression; increment) { // Loop body } The for in Matlab is very much more limited. The for structure in Matlab N = 10; for i = 1:N % Loop body end translates to C++ as follows { // New scope const int N = 10; for (int i = 1; i <= N; i = i + 1) { // Loop body } } You can't get "creative" with the logical expression as we have seen in numerous examples in C++. You can, however, use the while looping structure in Matlab to emulate anything you did in C++. %} >> >> whos Name Size Bytes Class i 1x1 8 double array j 1x1 8 double array s 1x1 8 double array Grand total is 3 elements using 24 bytes >> ls % You can use some unix commands in the Matlab command window % List of files in your current working directory >> ls -l % More complete listing info >> ls -al % Shows hidden files as well >> pwd ans = /afs/umich.edu/% Whatever directory you're in >> help system SYSTEM Execute system command and return result. [status,result] = SYSTEM('command') calls upon the operating system to execute the given command. The resulting status and standard output are returned. Examples: [status,result] = system('dir') [status,result] = system('ls') returns status = 0 and, in result, a MATLAB string containing a list of files in the current directory (assuming your operating system knows about the "dir" or "ls" command). If "dir" or "ls" fails or does not exist on your system, SYSTEM returns a nonzero value in status, and an explanatory message in result. See also DOS, UNIX, and ! (exclamation point) under PUNCT. Reference page in Help browser doc system >> clc % Clears the screen >> r = rand % A single random number between 0 and 1 inclusive r = 0.8913 >> whos Name Size Bytes Class r 1x1 8 double array Grand total is 1 element using 8 bytes >> r = rand(1,6) r = 0.7621 0.4565 0.0185 0.8214 0.4447 0.6154 >> r = rand(20,1) r = 0.7919 0.9218 0.7382 0.1763 0.4057 0.9355 0.9169 0.4103 0.8936 0.0579 0.3529 0.8132 0.0099 0.1389 0.2028 0.1987 0.6038 0.2722 0.1988 0.0153 >> r = rand(3,4) r = 0.7468 0.4660 0.5252 0.8381 0.4451 0.4186 0.2026 0.0196 0.9318 0.8462 0.6721 0.6813 >> r = rand(5,5) r = 0.3795 0.3046 0.5417 0.8537 0.6449 0.8318 0.1897 0.1509 0.5936 0.8180 0.5028 0.1934 0.6979 0.4966 0.6602 0.7095 0.6822 0.3784 0.8998 0.3420 0.4289 0.3028 0.8600 0.8216 0.2897 >> r = rand(5) r = 0.3412 0.5681 0.6946 0.8801 0.8757 0.5341 0.3704 0.6213 0.1730 0.7373 0.7271 0.7027 0.7948 0.9797 0.1365 0.3093 0.5466 0.9568 0.2714 0.0118 0.8385 0.4449 0.5226 0.2523 0.8939 >> randint(5,6,10) ans = 1 0 3 5 6 6 2 9 4 6 4 0 6 5 2 2 5 4 2 4 5 3 7 3 4 5 7 7 0 8 >> zeros(3,4) ans = 0 0 0 0 0 0 0 0 0 0 0 0 >> z = zeros(4,5) z = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> o = ones(3,7) o = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >> id = eye(5) id = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 >> id = eye(5,6) id = 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 >> clear >> r = rand(5) r = 0.0150 0.4387 0.9601 0.4399 0.6288 0.7680 0.4983 0.7266 0.9334 0.1338 0.9708 0.2140 0.4120 0.6833 0.2071 0.9901 0.6435 0.7446 0.2126 0.6072 0.7889 0.3200 0.2679 0.8392 0.6299 >> r(1,1) ans = 0.0150 >> r(4,3) ans = 0.7446 >> whos Name Size Bytes Class ans 1x1 8 double array r 5x5 200 double array Grand total is 26 elements using 208 bytes >> r r = 0.0150 0.4387 0.9601 0.4399 0.6288 0.7680 0.4983 0.7266 0.9334 0.1338 0.9708 0.2140 0.4120 0.6833 0.2071 0.9901 0.6435 0.7446 0.2126 0.6072 0.7889 0.3200 0.2679 0.8392 0.6299 >> rv = r(2,:) rv = 0.7680 0.4983 0.7266 0.9334 0.1338 >> cv = r(:,4) cv = 0.4399 0.9334 0.6833 0.2126 0.8392 >> r r = 0.0150 0.4387 0.9601 0.4399 0.6288 0.7680 0.4983 0.7266 0.9334 0.1338 0.9708 0.2140 0.4120 0.6833 0.2071 0.9901 0.6435 0.7446 0.2126 0.6072 0.7889 0.3200 0.2679 0.8392 0.6299 >> rv = r(end,:) rv = 0.7889 0.3200 0.2679 0.8392 0.6299 >> rv = r(end-1,:) rv = 0.9901 0.6435 0.7446 0.2126 0.6072 >> r r = 0.0150 0.4387 0.9601 0.4399 0.6288 0.7680 0.4983 0.7266 0.9334 0.1338 0.9708 0.2140 0.4120 0.6833 0.2071 0.9901 0.6435 0.7446 0.2126 0.6072 0.7889 0.3200 0.2679 0.8392 0.6299 >> r(2,2) = 0 r = 0.0150 0.4387 0.9601 0.4399 0.6288 0.7680 0 0.7266 0.9334 0.1338 0.9708 0.2140 0.4120 0.6833 0.2071 0.9901 0.6435 0.7446 0.2126 0.6072 0.7889 0.3200 0.2679 0.8392 0.6299 >> r(2:4,2:4) = zeros(3) r = 0.0150 0.4387 0.9601 0.4399 0.6288 0.7680 0 0 0 0.1338 0.9708 0 0 0 0.2071 0.9901 0 0 0 0.6072 0.7889 0.3200 0.2679 0.8392 0.6299 >> zeros(3) ans = 0 0 0 0 0 0 0 0 0 >> r(2:4,2:4) = zeros(4) ??? Subscripted assignment dimension mismatch. >> r r = 0.0150 0.4387 0.9601 0.4399 0.6288 0.7680 0 0 0 0.1338 0.9708 0 0 0 0.2071 0.9901 0 0 0 0.6072 0.7889 0.3200 0.2679 0.8392 0.6299 >> clear >> r = randint(5,6,4) r = 1 1 0 0 2 1 2 0 2 2 2 0 1 1 2 0 0 2 0 2 0 1 1 2 0 0 0 2 1 2 >> length(r) ans = 6 >> size(r) ans = 5 6 >> [Nrows,Ncols] = size(r) Nrows = 5 Ncols = 6 >> r = randint(5,6,4) r = 1 3 3 0 0 1 2 1 3 2 3 1 0 1 0 0 1 2 1 3 0 3 1 0 2 0 0 0 1 0 >> clear >> r = rand(5) r = 0.4586 0.8729 0.8704 0.8903 0.1556 0.8699 0.2379 0.0099 0.7349 0.1911 0.9342 0.6458 0.1370 0.6873 0.4225 0.2644 0.9669 0.8188 0.3461 0.8560 0.1603 0.6649 0.4302 0.1660 0.4902 >> ri = 1/r ??? Error using ==> mrdivide Matrix dimensions must agree. >> ri = eye(5)/r ri = 1.0720 -18.1453 19.3597 12.9173 -32.5026 0.2134 2.1249 -2.3719 -5.5488 10.8359 1.6087 -14.6343 14.2589 12.4468 -28.8248 -0.9060 19.9746 -20.0417 -12.8082 32.1340 -1.7447 9.1269 -8.8370 -3.2813 12.3797 >> ri * r ans = 1.0000 0.0000 -0.0000 0.0000 0 -0.0000 1.0000 0.0000 0 0 0.0000 0.0000 1.0000 0.0000 0 0.0000 0 0.0000 1.0000 0 -0.0000 0 0.0000 0 1.0000 >> format long >> ri*r ans = 1.00000000000000 0.00000000000000 -0.00000000000000 0.00000000000000 0 -0.00000000000000 1.00000000000000 0.00000000000000 0 0 0.00000000000000 0.00000000000000 1.00000000000000 0.00000000000000 0 0.00000000000000 0 0.00000000000001 1.00000000000000 0 -0.00000000000000 0 0.00000000000000 0 1.00000000000000 >> clear >> i = 0 i = 0 >> whos Name Size Bytes Class i 1x1 8 double array Grand total is 1 element using 8 bytes >> i = i + 1 i = 1 >> bitmax ans = 9.007199254740991e+15 >> j = bitmax j = 9.007199254740991e+15 >> j = j +1 j = 9.007199254740992e+15 >> j = j +1 j = 9.007199254740992e+15 >> j = j - 1 j = 9.007199254740991e+15 >> format hex >> j = bitmax j = 433fffffffffffff >> j = j + 1 j = 4340000000000000 >> j = j - 1 j = 433fffffffffffff >> j = nan j = fff8000000000000 >> format long >> j j = NaN >> j = -j j = NaN >> format hex >> j j = 7ff8000000000000 >> j = inf j = 7ff0000000000000 >> j = -j j = fff0000000000000 >> j = 0 j = 0000000000000000 >> j = -j j = 8000000000000000 >> format long >> exit >> clc >> v = randint(1,6,10) v = 9 2 6 4 8 7 >> whos Name Size Bytes Class v 1x6 48 double array Grand total is 6 elements using 48 bytes >> v(end + 1) = 99 % Works like a v.push_back(99) in C++ v = 9 2 6 4 8 7 99 >> v(end + 1) = 99 v = 9 2 6 4 8 7 99 99 >> % How do we do a pop_back() >> v = v(1:end - 1) % Works like a v.pop_back() in C++ v = 9 2 6 4 8 7 99 >> v = v(1:end - 1) v = 9 2 6 4 8 7 >> v = v(1:end - 1) v = 9 2 6 4 8 >> v = v(1:end - 1) v = 9 2 6 4 >> v = [v(1) 137 137 v(2:end)] % Horizontal concatonation v = 9 137 137 2 6 4 >> help horzcat HORZCAT Horizontal concatenation. [A B] is the horizontal concatenation of matrices A and B. A and B must have the same number of rows. [A,B] is the same thing. Any number of matrices can be concatenated within one pair of brackets. Horizontal and vertical concatenation can be combined together as in [1 2;3 4]. [A B; C] is allowed if the number of rows of A equals the number of rows of B and the number of columns of A plus the number of columns of B equals the number of columns of C. The matrices in a concatenation expression can themselves by formed via a concatenation as in [A B;[C D]]. These rules generalize in a hopefully obvious way to allow fairly complicated constructions. N-D arrays are concatenated along the second dimension. The first and remaining dimensions must match. C = HORZCAT(A,B) is called for the syntax '[A B]' when A or B is an object. Y = HORZCAT(X1,X2,X3,...) is called for the syntax '[X1 X2 X3 ...]' when any of X1, X2, X3, etc. is an object. See also VERTCAT, CAT. Overloaded functions or methods (ones with the same name in other directories) help inline/horzcat.m help timer/horzcat.m help serial/horzcat.m help avifile/horzcat.m help audiorecorder/horzcat.m help audioplayer/horzcat.m help gf/horzcat.m help zpk/horzcat.m help tf/horzcat.m help ss/horzcat.m help lti/horzcat.m help frd/horzcat.m help dynamicsys/horzcat.m help fittype/horzcat.m help idss/horzcat.m help idproc/horzcat.m help idpoly/horzcat.m help idmodel/horzcat.m help idgrey/horzcat.m help idfrd/horzcat.m help iddata/horzcat.m help idarx/horzcat.m help visa/horzcat.m help udp/horzcat.m help tcpip/horzcat.m help iviconfigurationstore/horzcat.m help icgroup/horzcat.m help icdevice/horzcat.m help gpib/horzcat.m help laurpoly/horzcat.m Reference page in Help browser doc horzcat >> more on >> help vertcat VERTCAT Vertical concatenation. [A;B] is the vertical concatenation of matrices A and B. A and B must have the same number of columns. Any number of matrices can be concatenated within one pair of brackets. Horizontal and vertical concatenation can be combined together as in [1 2;3 4]. [A B; C] is allowed if the number of rows of A equals the number of rows of B and the number of columns of A plus the number of columns of B equals the number of columns of C. The matrices in a concatenation expression can themselves by formed via a concatenation as in [A B;[C D]]. These rules generalize in a hopefully obvious way to allow fairly complicated constructions. N-D arrays are concatenated along the first dimension. The remaining dimensions must match. C = VERTCAT(A,B) is called for the syntax '[A; B]' when A or B is an object. Y = VERTCAT(X1,X2,X3,...) is called for the syntax '[X1; X2; X3; ...]' when any of X1, X2, X3, etc. is an object. See also HORZCAT, CAT. Overloaded functions or methods (ones with the same name in other directories) help timer/vertcat.m help serial/vertcat.m help avifile/vertcat.m help audiorecorder/vertcat.m help audioplayer/vertcat.m help gf/vertcat.m help zpk/vertcat.m help tf/vertcat.m help ss/vertcat.m help lti/vertcat.m help frd/vertcat.m help dynamicsys/vertcat.m help fittype/vertcat.m help idss/vertcat.m help idpoly/vertcat.m help idmodel/vertcat.m help idgrey/vertcat.m help idfrd/vertcat.m help iddata/vertcat.m help idarx/vertcat.m help visa/vertcat.m help udp/vertcat.m help tcpip/vertcat.m help iviconfigurationstore/vertcat.m help icdevice/vertcat.m help gpib/vertcat.m help laurpoly/vertcat.m Reference page in Help browser doc vertcat >> a = ... zeros(2) a = 0 0 0 0 >> % ... is a continution >> a = ... zeros(2) ones(3) a = 1 1 1 1 1 1 1 1 1 >> % Use space after ... for comments >> stuff whos Name Size Bytes Class firstPass 1x1 1 logical array n 1x1 8 double array Grand total is 2 elements using 9 bytes >> n n = 11 >> firstPass = true; >> n = 0; >> while (n <= 10 | firstPass) n = n + 1; if (firstPass) firstPass= false; end end >> n n = 11 >> a = rand(4,3) a = 0.4057 0.8936 0.0099 0.9355 0.0579 0.1389 0.9169 0.3529 0.2028 0.4103 0.8132 0.1987 >> a(end+1,2) = 137 a = 0.4057 0.8936 0.0099 0.9355 0.0579 0.1389 0.9169 0.3529 0.2028 0.4103 0.8132 0.1987 0 137.0000 0 >> a(3,end+1) = 314 a = 0.4057 0.8936 0.0099 0 0.9355 0.0579 0.1389 0 0.9169 0.3529 0.2028 314.0000 0.4103 0.8132 0.1987 0 0 137.0000 0 0 >> a(end+1,end+1) = 517 a = 0.4057 0.8936 0.0099 0 0 0.9355 0.0579 0.1389 0 0 0.9169 0.3529 0.2028 314.0000 0 0.4103 0.8132 0.1987 0 0 0 137.0000 0 0 0 0 0 0 0 517.0000 >> >> a = rand(3) a = 0.6038 0.0153 0.9318 0.2722 0.7468 0.4660 0.1988 0.4451 0.4186 >> b = [randint(3,3,10), ones(3) ; zeros(3) a] b = 8.0000 6.0000 6.0000 1.0000 1.0000 1.0000 5.0000 8.0000 3.0000 1.0000 1.0000 1.0000 2.0000 0 8.0000 1.0000 1.0000 1.0000 0 0 0 0.6038 0.0153 0.9318 0 0 0 0.2722 0.7468 0.4660 0 0 0 0.1988 0.4451 0.4186 >> >> russianRoulette Let's play Russian Roulette! ********************** With each spin you could win $1. But, if you lose, you lose everything! It's best to quit while you're ahead. It's also best to quit while you still have a head. Press any key to start. Good luck! Play? (0 = no, 1 = yes) :0 Wise choice. Chickenpoop, but wise :-) Your gain was $0 End of Game >> russianRoulette Let's play Russian Roulette! ********************** With each spin you could win $1. But, if you lose, you lose everything! It's best to quit while you're ahead. It's also best to quit while you still have a head. Press any key to start. Good luck! Play? (0 = no, 1 = yes) :11  Spin.... Click! You WIN! Your gain is $1 Repeat? (0 = no, 1 = yes) :1 Spin.... BANG! You LOSE! >> russianRoulette Let's play Russian Roulette! ********************** With each spin you could win $1. But, if you lose, you lose everything! It's best to quit while you're ahead. It's also best to quit while you still have a head. Press any key to start. Good luck! Play? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $1 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $2 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $3 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $4 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $5 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $6 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $7 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $8 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $9 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $10 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $11 Repeat? (0 = no, 1 = yes) :1 Spin.... Click! You WIN! Your gain is $12 Repeat? (0 = no, 1 = yes) :1 Spin.... BANG! You LOSE! >> type russianRoulette.m %RUSSIANROULETTE russianRoulette is a game of chance % % This is an example of a script M-file. %File: russianRoulette.m % Good was to start off a script clear all % Clear everything echo off % Turn off echo (turn on to debug) clc % Clear the command window prob = 1/6; % One bullet in a six chamber gun (chance of losing) disp(' ') disp('Let''s play Russian Roulette!') % Note '' to print ' disp('**********************') disp(' ') disp('With each spin you could win $1.') disp('But, if you lose, you lose everything!') disp('It''s best to quit while you''re ahead.') disp('It''s also best to quit while you still have a head.') disp('Press any key to start.') disp(' ') pause % Wait for user to decide to chicken out money = 0; repeat = input('Play? (0 = no, 1 = yes) :'); if (~repeat) disp('Wise choice. Chickenpoop, but wise :-)') end while(repeat) disp('Spin....') pause(1) % Pause for chamber to come to a rest if (rand < prob) % Note: rand alone is same as rand(1) disp('BANG! You LOSE!') return else disp('Click! You WIN!') money = money + 1; disp(sprintf('Your gain is $%g',money)) % The ugly sprintf function end repeat = input('Repeat? (0 = no, 1 = yes) :'); end disp(sprintf('Your gain was $%g',money)) disp('End of Game') disp(' ') % Skips a line >> clear all >> exit % Leaves Matlab, returns to Unix prompt