Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.

Similar presentations

Presentation on theme: "Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and."— Presentation transcript:


2 Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and columns, and known by a single name. 2. The size of an array is specified by the number of rows and the number of columns in the array, with the number of rows mentioned first. 3. Individual data values within an array are accessed by including the name of the array followed by subscripts in parentheses that identify the row and column of the particular value. 4. Even scalars are treated as arrays by MATLAB—they are simply arrays with only one row and one column. Arrays can be classified as either vectors or matrices. The term “vector” is usually used to describe an array with only one dimension, while the term “matrix” is usually used to describe an array with two or more dimensions.

3 MATLAB Variable A MATLAB variable is a region of memory containing an array, which is known by a user-specified name. The contents of the array may be used or modified at any time by including its name in an appropriate MATLAB command. MATLAB variable names must begin with a letter, followed by any combination of letters, numbers, and the underscore (_) character. Only the first 63 characters are significant; if more than 63 are used, the remaining characters will be ignored. If two variables are declared with names that only differ in the 64th character, MATLAB will treat them as the same variable. The most common types of MATLAB variables are double and char. MATLAB is a weakly typed language. Variables may be created at any time by simply assigning values to them, and the type of data assigned to the variable determines the type of variable that is created.

4 Double - MATLAB Variable
Type double is the principal numerical data type in MATLAB Variables of type double consist of scalars or arrays of 64-bit double- precision floating-point numbers. They can hold real, imaginary, or complex values. The real and imaginary components of each variable can be positive or negative numbers in the range to , with 15 to 16 significant decimal digits of accuracy. 4. A variable of type double is automatically created whenever a numerical value is assigned to a variable name. A real value is just a number. Eg;- var = 10.5 An imaginary number is defined by appending the letter i or j to a number. Eg;- var = 4i A complex value has both a real and an imaginary component. It is created by adding a real and an imaginary number together. Eg;- var = i

5 Char - MATLAB Variable Variables of type char consist of scalars or arrays of 16-bit values, each representing a single character. 2. Arrays of this type are used to hold character strings. They are automatically created whenever a single character or a character string is assigned to a variable name. Eg;- comment = 'This is a character string'

6 Creating & Initializing Variable
MATLAB variables are automatically created when they are initialized. There are three common ways to initialize a variable in MATLAB: 1. Assign data to the variable in an assignment statement. 2. Input data into the variable from the keyboard. 3. Read data from a file. Assigning data to variable in an assignment statement An assignment statement has the general form var = expression; var is the name of a variable. The expression is a scalar constant, an array, or a combination of constants, other variables, and mathematical operations (+,- ,*, etc.). The value of the expression is calculated using the normal rules of mathematics, and the resulting values are stored in named variable. The semicolon at the end of the statement is optional. Examples of initializing variables with assignment statements include var = 40i; var2 = var/5; x = 1; y = 2; array = [ ];

7 Creating & Initializing Array
Arrays are constructed using brackets ([]) and semicolons. the values in each row are listed from left to right, with the topmost row first and the bottommost row last. Individual values within a row are separated by blank spaces or commas, and the rows themselves are separated by semicolons or new lines. The following expressions are all legal arrays that can be used to initialize a variable:

8 Creating & Initializing Array
Yet more examples:- The number of elements in every row of an array must be the same, and the number of elements in every column must be the same. An expression such as [1 2 3; 4 5]; is illegal because row 1 has three elements while row 2 has only two elements. The expressions used to initialize arrays can include algebraic operations and all of or portions of previously defined arrays. a = [0 1+7]; b = [a(2) 7 a]; will define an array a [0 8] and an array b [ ]. If c is not previously defined, the statement c(2,3) = 5; will produce the matrix S Similarly, an array can be extended by specifying a value for an element beyond the currently defined size. For example, suppose that array d [1 2] . Then the statement d(4) = 4; will produce the array d [ ].

9 Creating & Initializing Array
Shortcuts It is easy to create small arrays by explicitly listing each term in the array, but It is just not practical to write out each element in the array separately for a large array. MATLAB provides a special shortcut notation for these circumstances using the colon operator. The colon operator specifies a whole series of values by specifying the first value in the series, the stepping increment, and the last value in the series. The general form of a colon operator is first:incr:last Eg:- » x = 1:2:10 x = Eg:- With colon notation, an array can be initialized to have the hundred values ,pi/100,2pi/100… follows: angles = (0.01:0.01:1.00) * pi; Eg:-Shortcut expressions can be combined with the transpose operator (') to initialize column vectors and more complex matrices. f = [1:4]'; generates a 4-element row vector [ ], and then transposes it into the 4 element column vector

10 Creating & Initializing Array
h = [g' g']; will produce the matrix Array creation and Initialization Using Functions Arrays can also be initialized using built-in MATLAB functions. If the function has a single scalar argument, it will produce a square array using the single argument as both the number of rows and the number of columns. If the function has two scalar arguments, the first argument will be the number of rows, and the second argument will be the number of columns. Since the size function returns two values containing the number of rows and columns in an array. Some examples using the zeros function follow: a = zeros(2); b = zeros(2,3); c = [1 2; 3 4]; d = zeros(size(c));

11 Creating & Initializing Array MATLAB functions for initializing Arrays

12 Creating & Initializing Variable
2. Input data into the variable using Keyboard It is also possible to prompt a user and initialize a variable with data that he or she types directly at the keyboard. This option allows a script file to prompt a user for input data values while it is executing. The input function displays a prompt string in the Command Window and then waits for the user to type in a response. my_val = input('Enter an input value:'); If the user enters a single number, it may just be typed in. If the user enters an array, it must be enclosed in brackets. In either case, whatever is typed will be stored in variable my_val when the return key is entered. If only the return key is entered, then an empty matrix will be created and stored in the variable. Eg1. » in1 = input('Enter data: '); Enter data: 1.23 stores the value 1.23 into in1. Eg2. » in2 = input('Enter data: ','s'); stores the character string '1.23' into in2.

13 Creating & Initializing Variable
3. Read data from file There are many ways to load and save data files in MATLAB. The save command saves data from the current MATLAB workspace into a disk file. Eg:- save filename var1 var2 var3 where filename is the name of the file where the variables are saved, and var1, var2, etc. are the variables to be saved in the file. By default, the file name will be given the extension “mat,” and such data files are called MATfiles. If no variables are specified, then the entire contents of the workspace are saved. MATLAB saves MAT-files in a special compact format that preserves many details, including the name and type of each variable, the size of each array, and all data values. A MAT-file created on any platform (PC, Mac, Unix, or Linux) can be read on any other platform, so MAT-files are a good way to exchange data between computers if both computers run MATLAB.

14 Creating & Initializing Variable
Unfortunately, the MAT-file is in a format that cannot be read by other programs. If data must be shared with other programs, then the -ascii option should be specified, and the data values will be written to the file as ASCII character strings separated by spaces. However, the special information such as variable names and types are lost when the data is saved in ASCII format, and the resulting data file will be much larger. For example, suppose the array x is defined as x=[ ; ]; The the command “save -ascii x.dat x” will produce a file named x.dat containing the following data: e e e+000 e e e+000 This data is in a format that can be read by spreadsheets or by programs written in other computer languages, so it makes it easy to share data between MATLAB programs and other applications.

15 Creating & Initializing Variable
The load command loads data from a disk file into the current MATLAB workspace. Eg:- load filename where filename is the name of the file to be loaded. If the file is a MAT-file, then all of the variables in the file will be restored, with the names and types the same as before. If a list of variables is included in the command, then only those variables will be restored. If the given filename has no extent, or if the file extent is .mat, then the load command will treat the file as a MAT-file. MATLAB can load data created by other programs in comma- or space separated ASCII format. If the given filename has any file extension other than .mat, then the load command will treat the file as an ASCII file. The contents of an ASCII file will be converted into a MATLAB array having the same name as the file that the data was loaded from. file named x.dat contains the following data: Then the command “load x.dat” will create a 2x3 array named x in the current workspace, containing these data values.

16 Creating & Initializing Variable
The load statement can be forced to treat a file as a MAT-file by specifying the –mat option. load –mat x.dat would treat file x.dat as a MAT-file even though its file extent is not .mat. Similarly, the load statement can be forced to treat a file as an ASCII file by specifying the –ascii option. These options allow the user to load a file properly even if its file extent doesn’t match the MATLAB conventions.

Download ppt "Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and."

Similar presentations

Ads by Google