Working with Matrices

Matrices with Multiple Elements

To enter a matrix having multiple elements, use braces ({ }) to enclose the data values and, if needed, use commas to separate rows. Inside the braces, all elements must be either numeric or character. You cannot have a mixture of data types within a matrix. Each row must have the same number of elements.

For example, suppose you have one week of data on daily coffee consumption (cups per day) of the four people in your office. Create a matrix called COFFEE with each person's consumption represented by a row of the matrix and each day represented by a column. First, use the PRINT option of the RESET command so that results are displayed as you submit statements. Next, enter the data into the matrix COFFEE. Here is the code:

  
    >  reset print; 
    >  coffee={4 2 2 3 2, 
    >          3 3 1 2 1, 
    >          2 1 0 2 1, 
    >          5 4 4 3 4}; 
  
            COFFEE        4 rows      5 cols    (numeric) 
  
                    4         2         2         3         2 
                    3         3         1         2         1 
                    2         1         0         2         1 
                    5         4         4         3         4
 
Now create a character matrix called NAMES with rows containing the names of the people in your office. Notice that when you do not use quotes, characters are converted to uppercase. Here is the code:

  
    >  names={Jenny, Linda, Jim, Samuel}; 
  
            NAMES         4 rows      1 col     (character, size 6) 
  
                                  JENNY 
                                  LINDA 
                                  JIM 
                                  SAMUEL
 
Notice that the output with the RESET PRINT statement includes the dimension, the type, and (when type is character) the element size of the matrix. The element size represents the length of each string, and it is determined from the length of the longest string.

Now display the COFFEE matrix with NAMES as row labels by specifying the ROWNAME= option in the PRINT statement. Here is the code:

  
    >  print coffee [rowname=names]; 
  
      COFFEE 
      JENNY          4         2         2         3         2 
      LINDA          3         3         1         2         1 
      JIM            2         1         0         2         1 
      SAMUEL         5         4         4         3         4
 

Previous Page | Next Page | Top of Page