Previous | Contents | Index |
The LET statement assigns values to individual array elements. For example:
DIM voucher_num%(100) . . . LET voucher_num%(20) = 3253% . . . END |
You can also assign values to a portion of an array with the LET statement and a FOR...NEXT loop. In the following example, the FOR...NEXT loop assigns zero to array elements (1,5) to (1,10), (2,5) to (2,10), and (3,5) to (3,10):
DIM po_number%(100,100) . . . FOR I% = 1% TO 3% FOR J% = 5% TO 10% LET po_number%(I%,J%) = 0% NEXT J% NEXT I% . . . END |
You print individual array elements by naming those elements in the PRINT statement. For example:
PRINT parts_list$(35%) |
With a FOR...NEXT loop, you can print all or part of an array. For example:
DIM capture_ratio(10,10) . . . FOR Y% = 7% TO 10% FOR X% = 7% TO 10% PRINT capture_ratio(X%,Y%) NEXT X% NEXT Y% |
The MAT statements discussed in this section are not related to the MAT GRAPH and MAT PLOT graphics statements. For more information about these statements, see Programming with VAX BASIC Graphics. |
MAT statements let you assign values to or display entire arrays with a single statement. They also let you do the following:
MAT statements are valid only on arrays of one or two dimensions. When MAT statements execute, they use row and column zero to store intermediate calculations. This means that MAT statements can overwrite data stored in row and column zero of your arrays, and you should not depend on data in these elements if your program uses MAT statements.
MAT statements cannot be used with arrays that have lower bounds other than zero. An attempt to specify a lower bound other than zero for an array in a MAT statement results in a compile-time error. |
The default subscripts for arrays created implicitly with MAT statements are (10) or (10,10). The default is two dimensions. This means that if you create an array with a MAT statement and do not specify any subscripts, BASIC creates a two-dimensional, 11-by-11 array. If you specify a single subscript, BASIC creates a one-dimensional array with 11 elements.
Table 7-1 lists MAT statements and explains their functions.
Statement | Function |
---|---|
MAT | Assigns values of zero, 1, or a null string to array elements. Also copies the values of one array to another and performs matrix arithmetic. |
MAT READ | Assigns DATA statement values to array elements. |
MAT INPUT [#] | Assigns values to array elements from your terminal or a terminal-format file. |
MAT LINPUT [#] | Assigns string values to string array elements from your terminal or from a terminal-format file. |
MAT PRINT [#] | Displays the contents of an array on your terminal, or writes array element values to a terminal-format file. |
In the following example, the first MAT statement creates the string array z_array$ with eight rows and eight columns and assigns a null string to all elements. The second MAT statement redimensions the array to six rows and six columns. The third MAT statement adds the values in each corresponding element of arrays B and C and stores the values in the corresponding elements of array A.
MAT z_array$ = NUL$(7,7) MAT z_array$ = NUL$(5,5) MAT A = B + C END |
The MAT statement can create an array and optionally assign values to all elements in that array. By specifying one of the MAT statement keywords, you can initialize arrays in one of four ways. Table 7-2 lists the MAT statement keywords and their functions.
MAT Keyword | Function |
---|---|
ZER | Sets the value of all elements in a numeric array to zero. |
CON | Sets the value of all elements in a numeric array to 1, except those in row and column zero. |
IDN | Sets the array to the identity matrix, that is, it sets the value of all elements in real or integer arrays to zero, except for those elements on the diagonal from element (1,1) to element (n,n), where n is the largest subscript in the array. The elements on the diagonal are set to 1. IDN applies to square arrays only. |
NUL$ | Sets the value of all elements in a string array to the null string, except those in row and column zero. |
The array name can specify an existing array. MAT statements do not assign values to row and column zero.
Note that the MAT statement does not require subscripts. In the case of existing arrays:
When you are creating arrays with MAT:
DIM A(10,10), B(15), C(20,20) MAT A = ZER !Sets all elements of A to 0 MAT B = CON(10) !Sets elements of B to 1; redimensions B MAT C = IDN(10,10) !Redimensions C to 10x10 identity matrix PRINT "ARRAY A:" MAT PRINT A; PRINT PRINT "ARRAY B:" MAT PRINT B; PRINT PRINT "ARRAY C:" MAT PRINT C; |
ARRAY A: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ARRAY B: 1 1 1 1 1 1 1 1 1 1 ARRAY C: 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 |
The MAT READ statement assigns values from DATA statements to array elements. Subscripts define either the dimensions of the array being created or the new dimensions of an existing array; subscripts are optional in MAT READ statements.
If you do not provide enough data in DATA statements to fill the specified array, BASIC leaves the remaining array elements unchanged. If you provide more data values than there are array elements, BASIC assigns enough values to fill the array and leaves the DATA pointer at the next value.
In the following example, BASIC fills matrix B with the first four DATA items, fills matrix C with the next four DATA values, and leaves the DATA pointer at the ninth value in the DATA list:
MAT READ B(2,2) MAT READ C(2,2) PRINT PRINT "MATRIX B" PRINT PRINT MAT PRINT B; PRINT PRINT "MATRIX C" PRINT PRINT MAT PRINT C; DATA 1,2,3,4,5,6,7,8,9,10 END |
MATRIX B 1 2 3 4 MATRIX C 5 6 7 8 |
The MAT INPUT statement assigns values from your terminal to array elements. The MAT INPUT statement reads data from a terminal-format file and writes it to an array. The optional subscripts in a MAT INPUT statement define either the dimensions of the array being created implicitly or the new dimensions of an existing array. If you are implicitly creating the array, the value of a subscript cannot exceed 10.
The MAT INPUT statement requests data from your terminal, as does the INPUT statement; it prints a question mark (?) prompt that you can disable with the SET NO PROMPT statement and then enable with the SET PROMPT statement. However, you cannot include a string prompt with the MAT INPUT statement.
When you enter a series of values separated by commas, BASIC enters the values you supply into successive array elements by row, starting with element (1,1) and filling row 1 before starting row 2. If you provide fewer data items than there are elements, the remaining elements are unchanged. If you provide more items than there are elements, BASIC ignores the excess.
The MAT INPUT statement takes values from an open file and assigns them to the matrix elements by rows, starting with element (1,1). It fills the elements in row 1 before starting row 2. The file can have one or more values in each record; however, multiple values must be separated with commas.
In the following example, the open file on channel 3 contains the following data: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13. The MAT INPUT statement reads this data and uses it to fill the array A, filling in row 1 before beginning row 2. The MAT INPUT B(2,2) statement dimensions array B to 9 elements (0 to 2 in each dimension) and provides values for all the elements except those in row and column zero.
MAT INPUT #3, A PRINT MAT PRINT A; MAT INPUT B(2,2) PRINT MAT PRINT B; |
1 2 3 4 5 6 7 8 9 10 11 12 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ? 1,2,3,4 1 2 3 4 |
Note that the MAT PRINT statement does not print row and column zero. For more information about the MAT PRINT statement, see Section 7.6.5.
The MAT INPUT statement can also redimension an existing array.
DIM new_array%(5,5) MAT INPUT new_array%(2,4) MAT PRINT new_array%; END |
? 1,2,3,4,5,6,7,8 1 2 3 4 5 6 7 8 |
When entering values in response to MAT INPUT, you can enter an
ampersand (&) as the last character on the line and continue on the
next line.
7.6.4 MAT LINPUT [#] Statement
The MAT LINPUT statement assigns string values to string array elements. The MAT LINPUT statement reads string values from a terminal-format file and writes them to a string array.
The MAT LINPUT statement prompts for individual array elements. It fills the array by rows, starting with element (1,1). It assigns the line you supply (including commas, semicolons, and quotation marks, but excluding the line terminator) to an array element.
DIM emp_nam$(5,5) MAT LINPUT emp_nam$(2,2) PRINT emp_nam$(1,1) PRINT emp_nam$(1,2) PRINT emp_nam$(2,1) PRINT emp_nam$(2,2) END |
? SMITH ? JONES ? WHITE ? BLACK SMITH JONES WHITE BLACK |
By specifying the subscripts (2,2), MAT LINPUT redimensions the array to nine elements and overwrites the old values (assigning the values in the same manner as MAT INPUT; see Section 7.6.3). BASIC then prompts for these elements.
MAT LINPUT also excludes line terminators when assigning values to string array elements. MAT LINPUT places the values from the open file into the specified array, filling the array by rows, starting with element (1,1). If there are more values in the file than there are array elements, BASIC ignores the excess records. If there are fewer, BASIC assigns a null string to the remaining elements.
The following program reads 50 records from the open disk file and assigns them to the array named part_name$. If there are more than 50 records in the file, BASIC ignores the excess records. If there are fewer than 50 records, then BASIC fills the remaining elements of the array with the null string.
DIM part_name$(50) MAT LINPUT #1%, part_name$ |
The MAT PRINT statement prints some or all of an array's elements, excluding row and column zero. The MAT PRINT # statement takes values from an array by row, starting with element (1,1), and writes each element to a sequential record in the terminal-format file.
Subscripts are optional in MAT PRINT statements. If you do not specify subscripts, MAT PRINT displays the entire array, excluding row and column zero. If you specify subscripts, MAT PRINT displays the specified subset of the array. In the case of the MAT PRINT # statement, the subscripts determine how many array elements are written to the file. The MAT PRINT [#] statement does not redimension an existing array.
If the last character in the MAT PRINT [#] array list is a semicolon, BASIC begins each array row on a separate line. Data values on each line are packed together with no intermediate spaces. However, if the last character in the MAT PRINT [#] array list is a comma, BASIC begins each array row on a separate line and each data value in a separate print zone.
If there is neither a comma nor a semicolon after the array name, BASIC prints each array element on a separate line. In the following example, the first MAT PRINT statement does not end in a comma or semicolon, so each element is printed on a separate line. The second MAT PRINT statement prints the elements twice, the first time starting each element in a new print zone, and the second time leaving a space before and after each value. The MAT PRINT # statement sends the last two lines of output to a terminal-format file.
MAT INPUT A(5) PRINT MAT PRINT A PRINT MAT PRINT A, A; MAT PRINT #3, A, A; END |
? 5 5 0 0 0 0 5 0 0 0 0 5 0 0 0 0 |
MAT statements do not signal error messages when there are more data items than array elements to contain them or when there are fewer data items than array elements to contain them.
BASIC provides two functions that let you determine how much data the MAT statements transfer: NUM and NUM2.
For two-dimensional arrays, the NUM function returns an integer value specifying the row number of the last data item transferred, and the NUM2 function returns an integer value specifying the column number of the last data item transferred. For one-dimensional arrays, the NUM function returns the number of items entered, and the NUM2 function returns a zero.
With these functions, you can determine the number of items transferred from a terminal-format file. Note, however, that you cannot use the NUM and NUM2 functions to implicitly declare an array. In the following example, the terminal-format file EMP.DAT contains the values 1 to 17, inclusive. When these values are read with the MAT INPUT # statement, NUM and NUM2 represent the row and column number, respectively, of the last value read.
OPEN "EMP.DAT" FOR INPUT AS FILE #3% DIM emp_name$(5,5) MAT INPUT #3%, emp_name$ PRINT NUM, NUM2 END |
4 2 |
BASIC provides a special set of MAT statements for array computations. These statements enable you to add, subtract, and multiply matrices, and to assign values to elements. Note that if you specify an array without subscripts (for example, MAT A), the default is two dimensions.
BASIC also provides matrix functions to transpose and invert matrices and to find the determinant of a matrix you invert.
MAT operators do not operate on elements in row or column zero. |
MAT operators perform matrix assignment, addition, subtraction, and multiplication.
All of these operations use the keyword MAT, followed by an expression. If the array has not been previously dimensioned, these operations create an array. The created output array's dimensions depend on the operation performed but must be (10,10) or smaller.
You can use the MAT operators on arrays larger than (10,10) if the input and output arrays are explicitly created or received as a formal parameter. |
You can assign all values in one array to another array with the MAT statement. In the following example, each element of new_array is set to the corresponding element in old_array. The dimensions of new_array are also redimensioned to the dimensions of old_array.
MAT new_array = old_array |
You can add the elements of two arrays. In the following statement, the two input lists, first_list% and second_list%, must have identical dimensions. The elements of the new list, sum_list%, equal the sum of the corresponding elements in the input lists.
MAT sum_list% = first_list% + second_list% |
You can also subtract the elements of two arrays. The following program subtracts one array from another:
DIM first_array(30,30) DIM second_array(30,30) DIM difference_array(30,30) . . . MAT difference_array = first_array - second_array |
Each element of difference_array is the arithmetic difference
of the corresponding elements of the input arrays.
7.7.1.3 Multiplication
You can multiply the elements of two arrays, provided that the number of columns in the first array equals the number of rows in the second array. The resulting array contains the dot product of the two input arrays.
DIM A(2,2), B(2,2), C(2,2) A(1,1) = 1 A(1,2) = 2 A(2,1) = 3 A(2,2) = 4 B(1,1) = 5 B(1,2) = 6 B(2,1) = 7 B(2,2) = 8 MAT C = A * B MAT PRINT C |
19 22 43 50 |
You can also multiply a matrix by a scalar quantity. BASIC multiplies each element of the input array by the scalar quantity you supply. The output array has the same dimensions as the input array. Enclose the scalar quantity in parentheses. The following example multiplies the elements of inch_array by the inch-to-centimeter conversion factor and places these values in cm_array:
DIM inch_array(5), cm_array(5) MAT READ inch_array DATA 1,12,36,100,39.37 MAT cm_array = (2.54) * inch_array MAT PRINT cm_array, END |
2.54 30.48 91.44 254 99.9998 |
Previous | Next | Contents | Index |