• Aucun résultat trouvé

Copying Subarrays

Dans le document Programming Guide, (Page 124-133)

elements of Array33 into corresponding elements of Array55.

With Technical BASIC, you can also copy a subset of an array (herein called a "subarray") into

The example copies all of Array33 into columns 2 through 4 of rows 2 through 4 of Array55.

The rest of Array55 is not changed, and the array is not redimensioned.

In general, you can specify both starting and ending row and starting and ending column for both the operand (Array33 above) and result (Array55 above). Subsequent examples explain this more clearly.

This statement copies the value from each element of Operand55 into the corresponding element of Result55. Operand55 into the redimensioned Result55.

MAT Result55

=

Operand55(1:3.1:3)

This statement copies the third element of Vector into the element in row 3 of column 2 of Result55.

MAT Result55(3,2)

=

Vector(3)

Vector Result55

1 0 0 0 0 0

2 0 0 0 0 0

3 0 3 0 0 0

4 0 0 0 0 0

5 0 0 0 0 0

This statement copies the values from row 1 columns 2 through 4 of Operand55 into row 3 columns 1 through 3 of Resu1t55.

MAT Result55(3,1:3)

=

Operand55(1,2:4)

Operand55 Result55

11 12 13 14 15 0 0 0 0 0 21 22 23 24 25 0 0 0 0 0 31 32 33 34 35 12 13 14 0 0 41 42 43 44 45 0 0 0 0 0 51 52 53 54 55 0 0 0 0 0

This statement copies the values from rows 4 and 5 of column 1 of Operand55 into rows 2 and 3 of column 5 of Result55.

MAT Result55(2:3,5)

=

Operand55(4:5,1)

Operand55 Result55

11 12 13 14 15 0 0 0 0 0 21 22 23 24 25 0 0 0 o 41 31 32 33 34 35 0 0 0 o 51 41 42 43 44 45 0 0 0 0 0 51 52 53 54 55 0 0 0 0 0

This statement copies the entire Vector into the entire third row of Result55.

Summary of General Rules

• The array elements are always copied and assigned in row-major order - from the first column to the last column of each row, beginning with the first row and proceeding through the last row.

• If all elements of the result array are to be assigned values, then do not specify row numbers or column numbers in the result array.

• If all elements of the operand array are to be copied into the result array, then do not specify row numbers or column numbers in the operand array.

• If row and/or column numbers are specified, they must be enclosed in parentheses and separated by a comma.

• If no row or column numbers are specified after the result array, then it is redimensioned (if necessary) before values are assigned to it. If row or column numbers are specified after the result array, then values are assigned to the corresponding elements and the array is not redimensioned.

• If the array is a vector, then specify only the row number(s).

• If only one row is to be copied or assigned values, then you need only specify that one row number; if more than one row is to be copied or assigned values, then specify the first row number and the last row number, separated by a colon. Similarly, if only one column is to be copied or assigned values, then you need only specify that one column number;

if more than one column is to be copied or assigned values, then specify the first column number and the last column number, separated by a colon.

• If entire row(s) are to be copied or assigned values, then you may omit the column numbers but include a comma after the row number(s). Similarly, if entire column(s) are to be copied or assigned values, then you may omit the row numbers but include a comma before the column number(s).

• Unless either the operand array or the result array is a vector, the number of rows specified after the result array must be the same as the number of rows to be copied from the operand array. The number of columns specified after the result array must be the same as the number of columns to be copied from the operand array.

• Unless the operand array is a vector, a column from the operand array cannot be copied, using just one statement, into a row in the result array. Similarly, unless the result array is

In this example, row 1 of Array33 is copied into column 3 of array Result55, then column 3 of

The row and column number(s) can be specified not only as constants, like those in the preceding examples, but also as variables or expressions. Here is an example:

245 Column=2

250 MAT Vector3=Operand33(,Column)! Copy column 2 into vector.

255 BottomRow=3

260 MAT Result33(BottomRow-2,)=Vector3 Copy vector into row 1.

The first row (or column) number specified is usually less than the second row (or column)

A Special Case: Empty Arrays

Here is the special case mentioned earlier: When the first row specified is just one greater than the second row, and with the corresponding case for columns, then no elements will be copied or assigned values. Furthermore, if no row or column numbers are specified after the result array

Empty arrays can be specified in subsequent statements and functions with meaningful results; the usual rules of redimensioning and row/column matching apply (in statements with two operand arrays). The following situations are of particular interest:

• Statements specifying only one operand array will, if that array is empty, redimension the destination array to be empty. For example, if the Operand array has been redimensioned to a Ox3 array, then this statement redimensions the Result array to Ox3:

MAT Result

=

Operand

• If both operand arrays are empty, then performing a matrix multiplication1 can yield a result array that is not empty. However, in such cases the statement assigns the value 0 to all elements of the destination array, regardless of the current values in the operand arrays.

For example, if matrix OperandI has been redimensioned to be 3xO, and matrix Operand2 has been redimensioned to be 0 xl, then this statement:

MAT Result

=

Operand1*Operand2

redimensions matrix Result to 3 xl, since 3 X 0·0 X 1 =3 X 1 according to the rules of matrix multiplication. The Result array is not empty, since neither its number of rows nor number of columns is zero. However, it is a zero matrix, since the value 0 has been assigned to all (three) elements.

The fact that no elements are copied or assigned values when the first row or column number is just one greater than the second, plus the characteristics previously described above for resulting empty arrays, simplifies programs that do certain matrix manipulations.

Dans le document Programming Guide, (Page 124-133)

Documents relatifs