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

Task 2.3 Detail: Use matrix (table) variables to make calculations

Summary of new tools and commands.

Task: Create an array variable with 5 rows and 3 columns so that the first column is the width, the second is the height and the third is initially zero. Use a command to calculate the area from the chosen width and height in each row. Display the second row of this table, with appropriate labels.

The semicolon character (;) indicates the end of a row in the concatenation operation. So the command C = [ 1 2 3; 4 5 6] will create a matrix (table) variable with two rows and three columns. The first row is (1, 2, 3) and the second row is (4, 5, 6).

Specific values in matrix (table) variables are referred to by the row and column index of the desired position. So C(1,3) refers to the value in the first row and the third column.

Arithmetic operations can be done on individual parts of matrix variables. C(1,3)=C(1,1).*C(1,2) would multiply the values in the first and second column of the first row and save the value in the third column of the first row.

A list of values is created with the colon notation by giving a start value and a finish value such as 1:8 creates the list [1 2 3 4 5 6 7 8]. This notation assumes the interval between values is 1.

An extension to this notation allows specification of the interval. 10:10:60 is a list that starts at 10 with an interval of 10 until it reaches 60, or [10 20 30 40 50 60].

The list does not exceed the upper value, so the list 10:10:29 produces the list [10 20]. The command 10:10:31 produces the list [10 20 30].

This notation can be used to refer to a list of array values, as D(1:3,1) refers to the values in column 1 of the first 3 rows of the variable D.

Using : by itself indicates all values in that dimension. So, D(:,1) refers to all rows in the first column of D.

Script to complete this task:

%  task23.m
  RECT=[3 5 0; 4 8 0; 5 9 0; 10 12 0; 14 13 0];
  RECT(:,3)=RECT(:,1).*RECT(:,2);
  disp(['Width(2) = ' num2str(RECT(2,1))])
  disp(['Height(2) = ' num2str(RECT(2,2))])
  disp(['Area(2) = ' num2str(RECT(2,3))])

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


email: J. Klinck