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

Task 2.2 Detail: Using vector (list) variables to make calculations

Summary of new tools and commands.

Task: Create two variables holding five values of width and height for 5 rectangles. Calculate the area of each rectangle and save the result in a third variable. Also calculate the total area of all rectangles. Display at the command line the height, width and area of the fourth rectangle as well as the total area of all rectangles.

MATLAB was originally written to do matrix calculation; its name comes from "MATrix LABoratory". Any of the variables in MATLAB can be a scalar (a single number), a vector (a list of numbers) or a matrix (an array or table of numbers). There is no need to indicate before creating a variable, what shape and size it will be.

Because of this original intent, arithmetic acts over all parts of a variable. So if A and B are both 2x2 matrices (a table of numbers with 2 rows and 2 columns), then C = A + B will add the corresponding values in the two matrices to create the third matrix (C) which will also be 2x2. This operation over several values happens automatically (no need for looping commands as are needed in FORTRAN or C programming languages).

Subtraction, indicated by -, acts the same way as addition but subtracting the corresponding values in the two variables.

Because the simple multiplication operator (*) is matrix multiplication, there is a separate command (.*) which does multiplication element-by-element. In this way, you can multiply corresponding parts of multipart variables, as long as the size and shape of the two variables are the same.

The division operator (/) is a matrix inversion operator. The statement A/B will multiply A by the matrix inverse of B. Use ./ to get element-by-element division.

It is easy to raise a variable or an array of variables to a power with .^. So, the cube of a variable is created with the command z3 = z.^3.

In many arithmetic operations, it is important to be clear about which operations are applied to which variables. Parenthesis can be used for clarity. For example, z = (x+2).^2 will add 2 to x before squaring it. In contrast, z = x+2.^2 will add 2 squared (4) to the value of x. Similarly, y = x.^(n+2) will raise the value of x to the power of 2 more than the current value of n; while y = x.^n+2 will add 2 to the square of x.

One use of the concatenation operator is to create a vector variable. So the command A = [ 1 2 3 4 ] creates a vector variable A with 4 values. Specifically, it creates a row vector which has one row and four columns.

A column vector is created with B = [1; 2; 3; 4], where ; indicates the end of a row.

By default, every variable in MATLAB is a matrix so A is a 1x4 (row x column) matrix and B is a 4x1.

A matrix variable is a collection of values which can be referenced by the collective name. It is possible to refer to an individual value in the matrix by giving the row and column address of the individual value using parenthesis.

An individual value in a matrix (table) is referenced by its position with the following notation (A(row,column): A(3,2) is the value in the third row and second column of the variable A. So, A(3,2) = 10 changes that value the table to 10. The other values are not changed.

To print the value in the third row, second column of the variable, just type A(3,2) at the command line and the variable name and value would be displayed.

A second way to show a value is with the disp() command. This command only shows character strings, not numbers so there is another step.

A character string is created by putting characters between single quotes '. So typing the command disp('The value of A(3,2) is ') will print "The value of A(3,2) is " in the command window. This does not show the value of the variable A(3,2).

The command disp('The value of A(3,2) is ' A(3,2)) is a good guess but it will fail (for 2 reasons).

The first reason is that the disp function only takes a single character string so you need to use the concatenation operator to combine the different parts: disp(['The value of A(3,2) is ' A(3,2)]). But this command fails for the second reason because the variable A(3,2) is a number, not a character string.

The function num2str() converts a numerical value to the equivalent character string. So the correct command is disp(['The value of A(3,2) is ' num2str(A(3,2))]), which displays the message and the value as you requested.

Script to complete this task:

% task22.m
  width=[3 7 9 11 15];
  height=[.3 .8 1.9 2.3 5.1];
  RectArea=width.*height;
% there is a shorter, easier way to do this (coming soon).
  TotArea=RectArea(1)+RectArea(2)+RectArea(3)+RectArea(4)+RectArea(5);
  disp(['Total Area: ' num2str(TotArea)])
  disp(['Height, Width of fourth entry = ' num2str(height(4)) ' ' num2str(width(4))])
  disp(['Area of fourth entry = ' num2str(RectArea(4))])

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


email: J. Klinck