(^610) | Multidimensional Arrays and Numeric Computation
publicMatrix add(Matrix two) throwsMatException
// Returns the sum of matrix and two.matrix
{
if (matrix.length != two.matrix.length ||
matrix[0].length != two.matrix[0].length)
throw newMatException("Illegal matrix add.");
else
{
Matrix result = new Matrix(matrix.length, matrix[0].length);
for (int row = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[0].length; col++)
{
result.matrix[row][col] = matrix[row][col] +
two.matrix[row][col];
}
returnresult;
}
}
publicMatrix sub(Matrix two) throwsMatException
// Returns two.matrix subtracted from matrix
{
if (matrix.length != two.matrix.length ||
matrix[0].length != two.matrix[0].length)
throw newMatException("Illegal matrix subtract.");
else
{
Matrix result = new Matrix(matrix.length, matrix[0].length);
for (int row = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[0].length; col++)
{
result.matrix[row][col] = matrix[row][col] -
two.matrix[row][col];
}
returnresult;
}
}
sub(two)
ifthe subtraction is not legal
throw MatException
else
Create result matrix with the same dimensions as matrix
forrow going from 0 through matrix.length – 1
forcol going from 0 through matrix[0].length – 1
Set result[row][col] to matrix[row][col] two.matrix[row][col]
returnresult
T
E
A
M
F
L
Y
Team-Fly®
やまだぃちぅ
(やまだぃちぅ)
#1