|
|
|
Write a scalarMultiply( ) method that multiplies a matrix m with a number a
public static double[][] scalarMultiply(double[][] m, double a)
{
double[][] result = new double[m.length][m[0].length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m[0].length; j++)
{
result[i][j] = m[i][j] * a;
}
}
return result;
}
|
(1) Define a 2-dimensional array to store the result
public static double[][] scalarMultiply(double[][] m, double a)
{
double[][] result = new double[m.length][m[0].length];
// result matrix has
// same # rows and # columns as m
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m[0].length; j++)
{
result[i][j] = m[i][j] * a;
}
}
return result;
}
|
(2) Compute the result
public static double[][] scalarMultiply(double[][] m, double a)
{
double[][] result = new double[m.length][m[0].length];
// result matrix has
// same # rows and # columns as m
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[0].length; j++)
{
result[i][j] = m[i][j] * a;
}
}
return result;
}
|
(3) Return the result
public static double[][] scalarMultiply(double[][] m, double a)
{
double[][] result = new double[m.length][m[0].length];
// result matrix has
// same # rows and # columns as m
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[0].length; j++)
{
result[i][j] = m[i][j] * a;
}
}
return result;
}
|
public static void main()
{
double [][] a = { {1, 8, -3},
{4, -2, 5} };
double[][] b;
b = scalarMultiply(a, 2);
System.out.println( Arrays.toString(a[0]) );
System.out.println( Arrays.toString(a[1]) );
System.out.println( " * " + 2 + " = " );
System.out.println( Arrays.toString(b[0]) );
System.out.println( Arrays.toString(b[1]) );
}
|
DEMO: demo/09-multi-dim-array/05-matrix/ScalarMult.java
What happens when you transposes a matrix:
Original matrix: Transposed matrix:
+- -+ +- -+
| 1 2 3 | | 1 4 |
| 4 5 6 | | 2 5 |
+- -+ | 3 6 |
+- -+
Change 1: Number of rows: 2 Number of rows: 3 (exchanged !)
Number of columns: 3 Number of columns: 2
Change 2: Row and column indexes: (exchanged !)
+- -+ +- -+
| 0,0 0,1 0,2 | | 0,0 0,1 |
| 1,0 1,1 1,2 | | 1,0 1,1 |
+- -+ | 2,0 2,1 |
+- -+
|
Write a matrixTranspose( ) method that transposes a matrix M
public static double[][] matrixTranspose(double[][] m)
{
double[][] result = new double[m[0].length][m.length];
// result matrix has:
// # rows = # columns in m
// # columns = # rows in m
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
result[j][i] = m[i][j];
// Copy m's element in row i and column j
// to row j and column i in result
return result;
}
|
(1) Define a 2-dimensional matrix to store the result
public static double[][] matrixTranspose(double[][] m)
{
double[][] result = new double[m[0].length][m.length];
// result matrix has:
// # rows = # columns in m (= m[0].length)
// # columns = # rows in m (= m.length)
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
result[j][i] = m[i][j];
// Copy m's element in row i and column j
// to row j and column i in result
return result;
}
|
(2) Compute the transpose
public static double[][] matrixTranspose(double[][] m)
{
double[][] result = new double[m[0].length][m.length];
// result matrix has:
// # rows = # columns in m (= m[0].length)
// # columns = # rows in m (= m.length)
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
result[j][i] = m[i][j];
// Copy m's element in row i and column j
// to row j and column i in result
return result;
}
|
(3) Return the result
public static double[][] matrixTranspose(double[][] m)
{
double[][] result = new double[m[0].length][m.length];
// result matrix has:
// # rows = # columns in m (= m[0].length)
// # columns = # rows in m (= m.length)
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
result[j][i] = m[i][j];
// Copy m's element in row i and column j
// to row j and column i in result
return result;
}
|
public static void main()
{
double [][] a = { {1, 8, -3},
{4, -2, 5} };
double[][] b;
b = matrixTranspose(a);
System.out.println( Arrays.toString(a[0]) );
System.out.println( Arrays.toString(a[1]) );
System.out.println( "Transpose:" );
System.out.println( Arrays.toString(b[0]) );
System.out.println( Arrays.toString(b[1]) );
System.out.println( Arrays.toString(b[2]) );
}
|
DEMO: demo/09-multi-dim-array/05-matrix/TransposeMatrix.java