Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 2

Task 2.4 Detail: Creating and using multi-dimensional variables

Summary of new tools and commands.

Task: Create a variable with 8 rows each assigned the value of zero. Assign even integer values to this variable starting with 12. Calculate the square of each of these values and save them in another variable. Display the 5th value of these arrays with appropriate labels.

It is often useful to create an array of variables of the desired size and shape filled with some arbitrary values. Later operations can insert appropriate values. The command x = zeros(5,1) creates a variable called x with 5 rows and one column, each entry having the value of zero. Each value in this array can be changed with a command of the form x(2)=6.

The benefit of this creator is that MATLAB can create a multi-dimensional variable (vector or array) with a single operation. This method of creating an array is also more efficient.

It is possible to expand array variables automatically. A = [ 2 10 13]; creates a variable with three rows. The command A(4)=17; references a location beyond the size of the variable. Instead of reporting an error and stopping the calculation, MATLAB will expand the array to have 4 columns giving the fourth column the value you specified.

As you might imagine, this takes some time to do. If you know ahead of time that you want an array with 10 rows, then it is faster to create the array, filled with zeros, in one step and later specify the values that you want.

Most of the class scripts are so small that you are unlikely to see shorter run times with the more efficient procedure. There could be a real issue with later scripts using much larger arrays of variables. Creating array variables and then specifying the values is a good programming habit to develop.

Be careful with array creators, because of the following unexpected result. You would expect that x=zeros(5); would create a vector (list) variable with 5 values. However, recall that MATLAB is based on matrices, so this actually creates an array with 5 rows and 5 columns (a 5x5 array). If you want a list variable, you need to use the following: x=zeros(5,1); to create a column vector with 5 values or x=zeros(5,1); to create a row vector with 5 values.

There are other array creator functions, such as ones(r,c) which creates an array with the indicated number of rows and columns, each of which has the value one.

There are no other array creators of this form (so there is no function called tens(r,c)). But it is easy to create an array of 10's with the command z = 10*ones(5,2).

It is easy to erase (remove) a variable with the clear command which can take the form of clear x y z. This is a convenient way to clear out unnecessary temporary variables.

In working with MATLAB, it is useful to see what variables have been defined. Depending on your setup, there is likely a workspace pane which has some information about active variables.

There is a command, who, which will provide a list of all currently active variables. This just gives the names of the variables. A second command, whos, which provides not only the variable names, but their shape (rows and columns), and their type (real, integer, character, etc).

Script to complete this task:

% task24.m
  x=zeros(8,1);
  x(:)=12+2*(0:7);
  x2=x.^2;
  disp([' the square of ' num2str(x(5)) ' is ' num2str(x2(5)) ])

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 2


email: J. Klinck