Working with Variables and Arrays in MATLAB
The structural unit of data in any MATLAB program is the array. An array is a collection of record values organized into rows and columns and known by a single name. Arrays can be allocated as either vectors or matrices. The term “vector” is generally used to define an array with only one dimension, while the term “matrix” is usually used to determine an array with two or more dimensions.
The numbers of the row state the size of an array, and the numbers of the column in the array, with the numbers of the row, mentioned first. The total number of items in the array will be the product of the number of rows and the number of the column.
For example, the size of the following arrays are:
Individual items in an array are addressed by the array name followed by the row and column of the particular item. If the array is a row or column vectors, then only one subscripts are needed. For example, in the preceding array a(2,1) is 3 and c(2) = 2.
A MATLAB variable is an area of memory, including an array, which is called by a customer-specified name. The content of the array may be used or modified at any time by containing its name in an appropriate MATLAB command.
MATLAB variables name must start with a letter, followed by any sequence of letters, numbers, and the underscore (_) character. Only the first 63 characters are essential; if more than 63 are used, the remaining characters will be ignored. If two variables are stated with names that only differ in the 64th character, MATLAB will treat them as the same variable.
Creating and Initializing Variables in MATLAB
MATLAB variables are automatically generated when they are initialized.
There are three methods to initialize a variable in MATLAB:
- It assigns data to the variable in an assignment statement.
- It input data into the variable from the keyboard.
- It read data from a file.
Initializing Variables in Assignment Statement
The simplest method to initialize a variable is to assign it one or more value in an assignment statement.
An assignment statement has the standard form
var = expression;
where var is the name of the variables and expression is a scalar constant, an array, or a combination of constants, other variables, and mathematical operations (+, -, etc.). The value of the expression is computed using the standard rules of mathematics, and the resulting values are saved in the named variable. The semicolon at the last of the statement is optional. If the semicolon is absent, the values assigned to var will be echoed in the command window. If it is present, nothing will be shown in the Command Window, even though the assignment has appeared.
Examples of initializing variables with assignment statements contain
var = 40i;
var2 = var/5;
x = 1; y = 2;
array = [1 2 3 4];
The first example generates a scalar variable of type double and saves the imaginary number 40i in it.
The second example generates a scalar variable and saves the result of the expression var/5 in it.
The third example shows that multiple assignment statements can be placed on a single line, supported that they are divided by semicolons or commas.
The last example display that variables can also be initialized with arrays of data. Such arrays are build up using brackets () and semicolons. All of the items of an array are listed in row order. In other words, the value in each row are recorded from left to the right, with the top-most row first, and the bottom-most row last. The single value within a row are separated by blank spaces or commas, and the rows themselves are divided by semicolons or newlines.
The following statements are all legal arrays that can be used to initialize a variable:
The number of items in every row of an array must be the same, and the number of items in every column must be the same. An expression such as
[1 2 3; 4 5];
is illegal because row 1 has three items, while row 2 has only two items.
Initializing with Built-In Functions
Arrays can also be initialize using built-in MATLAB function. For example, the function zero can be used to generate an all-zero array of any desired size. There are a various form of the zeros function. If the function has an individual scalar argument, it will develop a square array using the single arguments as both the number of rows and the number of columns. If the function has two scalar argument, the first arguments will be the number of rows, and the second arguments will be the number of the columns. Since the size function return two values including the number of row and column in an array, it can be combined with the zero function to create an array of zeros that is the same size of another array.
Some examples using the zeros function follow:
a = zeros(2);
b = zeros(2,3);
c = [1 2; 3 4];
d = zeros(size(c));
These statements generate the following arrays:
Similarly, the ones function can be used to generate array including all ones, and the eye function can be used to generate arrays including identity matrices, in which all on-diagonal items are one, while all off-diagonal items are zero.
Following the tables containing a list of standard MATLAB functions useful for initializing variables.
MATLAB Functions Useful for Initializing Variables
|zeros(n)||Creates a n x n matrix of zeros.|
|zeros(m,n)||Creates a m x n matrix of zeros|
|zeros(size(arr))||Create a matrix of zeros of the same size as arr.|
|ones(n)||Creates a n x n matrix of ones.|
|ones(m,n)||Creates a m x n matrix of ones.|
|ones(size(arr))||Creates a matrix of ones of the same size as arr.|
|eye(n)||Creates a n x n identity matrix.|
|eye(m,n)||Creates an m x n identity matrix.|
|length(arr)||Return the length of a vector, or the longest dimension of a 2-D array.|
|size(arr)||Return two values specifying the number of rows and columns in arr.|
Initializing Variables with Keyboard Input
It is also possible to prompt a customer and initialize a variable with a record that he or she type directly at the keyboard. This option enables a script file to prompt a customer for input data value while it is executing. The input function shows a prompt string in the Command Window and then waits for the user to type in a response.
For example, consider the following statements:
my_val = input(‘Enter an input value:’);
When this function is executed, MATLAB prints out the string ‘Enter an input value:,’ and then waits for the client to respond. If the customer enters a single number, it may just be typed in. If the customer enter an array, it must be enclosed in brackets. In other 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 generated and stored in the variable.
If the input function contains the character ‘s’ as a second argument, then the input data is returned to the customer as a character string. Thus, the statement
» in1 = input(‘Enter data: ‘);
Enter data: 1.23
stores the value 1.23 into in1, while the statement
» in2 = input(‘Enter data: ‘,’s’);
Enter data: 1.23
save the character string ‘1.23’ into in2.