Link:http://output.to/sideway/default.asp?qno=140400002 MatLab Basics MatLab: BasicsMajor Reference Source: MatLab Verson 7.0 MatLab Package is designed for array or matrix operations, and therefore all data are stored in the form of arrays. ArraysAll types of data in MatLab are stored in the form of arrays. Arrays can have more than one dimension. For example
The size of an array refers to the number of maximum index in each diemension not the dimension of an array. For example, the size of a matrix is given by the number of rows and columns. VariablesIn general, a variable is used by MatLab to represent a specific piece of computer memory occupied by an array. A variable name, which can be specified by user, is given to each variable for the identification of variables stored in the memory. If no variable name is specified for a variable, a variable name, ans, is automatically created as the identification of variable stored the most recent evaluated result. A valid MatLab variable name have the following properties:
Array and Variable CreationSquare Brackets, [ ], are used to form an array, i.e. vectors, matrices and multi-dimensional arrays in MatLab. Entering a pair of square brackets, [ ] in the Command Window is the most simplest way of creating an array. As no user-specified variable name is assigned to the created array, the variable name, ans, is automatically generated by MatLab and is assigned to the created variable, the empty array. Array CreationIn general, an array is used by MatLab to represent the data structure or form for storing multiple elements of data in memory. The creation or construction of an array can be done by
Array Creation by entering elements within matrix constructor operator, [ ]The simplest way to construct an array is making use of the matrix constructor operator, that is a pair of square brackets, [ ], to input elements of matrix. The open square bracket is used to indicate the begin of a matrix, while the close square bracket is used to indecate the end of the matrix. Elements of the matrix are entered within the brackets. A matrix constructor operator can only be used to construct a matrix, that is a two dimensional array. A one dimensional array can also be interpreted as two dimenstional array with one dimension equals to one. Similarly, a empty array can be interpreted as a 0-by-0 matrix and a scalar can be interpreted as a 1-by-1 matrix. Therefore a matrix constructor operator can be used to construct a empty array, a scalar, or a vector also. Elements are entered row by row from left to right within the matrix constructor operator, [ ]. Both comma, ',' or space, ' ', can be used to separate each element within a row, while a semicolon, ';' or a newline are used to separate one row from other elements of another row within the matrix. When using space ' ' as element separator, the sign of a signed number must immediately precedes the numeric value. Examples
Array Creation by generating an array with elements of a numeric sequence by the colon operator, :The colon operator is an easy way to generate a numeric sequence in the form of a row array. Since an increasing unit-step sequence is commonly used in linear indexing and matrix construction, the simplest form of default colon operator is a simple increasing numeric sequence generator with default step +1 starting from the specified first value to the specified last value. Besides the default step of =1, a step value, either positive or negative, can also be specified. Syntaxsmallvalue : largetvalue startvalue : stepvalue : lastvalue Parameterssmallvalue : is to specified the start value of an increasing unit-step sequence. largevalue : is to specified the last value of an increasing unit-step sequence. startvalue : is to specified the start value of a numeric sequence. stepvalue : is to specified the step of a numeric sequence. lastvalue : is to specified the last value of a numeric sequence. Examples
Array Creation by using the builtin specialized array functionSince matrices or arrays can often be categorized according to the configurations of their elements or entries, MatLab provides some builtin specialized array functions that can be used to create some common kinds of matrices or arrays Examples
Array Creation by concatenating arrays within matrix constructor operator, [ ]Using the matrix constructor operator, [ ], to construct a matrix can be considered as the process of joining elements to form a matrix. The joining process of the matrix constructor operator can also be used as a matrix concatenation function to join one or more arrays to form a new array along one dimension of a matrix, that is to concatenate arrays horizontally or vertically. Examples
Array Creation by using the builtin specialized array concatenation functionBesides concatenating arrays within matrix constructor operator, MatLab also provides some builtin specialized array concatenation functions that can be used to concatenate existing arrays to from a new array. Examples
Variable CreationUser specified variables are created when these variables are initialized. Variable creation by initialization can be done by:
Variable Creation by statementAn assignment statement is used to store the elements of an evaluation result of an expression to the user specified name of the array of a variable. An equal sign, =, is used as assignment in MatLab. Syntaxname = expression Parametersname: is a valid MatLab variable name or named expression. expression: is an legal MatLab expression Examples
Variable Creation from keyboardSimilar to the assignment of evaluation result of a builtin function statement, the builtin input function is used as the expression to request and get the user input by displaying an input prompt on the Command Window. Then MatLab will pause and wait for input from the keyboard. The response to the input prompt can be any legal MatLab expression as in the assignment statement. The input response can be ended by press the Return or Enter key. The input response will be evaluated first and the evaluated result is then be assigned to user_entry Syntaxuser_entry = input('promp']) user_entry = input('promp'[,'s']) Parametersuser_entry: is a valid MatLab variable name or named expression. prompt: is the character string to be displayed on the screen as a prompt. s: is an optional parameter to indicate that the entered string is interpreted as text string variable rather than as variable name or numerical value. Examples
Variable Creation from a fileA load command can also be used to load variables from the file that containing needed variables which are created and saved in the data file before. If no filename is specified, the default file is a MAT-file of file name, matlab.mat. A user specified file can also be specified if the variables are stored with other file name. The load command can also retrieive only the specified variables from the data file by appending the needed variable names with space separator to the end of load command after the file name. The file name, even using the default file name, should be specified when loading only specified variables. If no variable name is specified, all variables in the data file will be retrieved. Syntaxload load FILENAME load FILENAME X Y Z ... ParametersFILENAME: is an optional parameter to specified the data file storing the needed variables. X Y Z ...: is the optional list of the needed variable names with space separator. Examples
|
Sideway BICK Blog 02/04 |