5.21 LAB: 1xN and NxN Matrix multiplication A matrix is a rectangle of numbers in rows and columns. A 1xN matrix has one row and N columns. An NxN matrix has N rows and N columns. Multiplying a 1xN matrix A and an NxN matrix B produces a 1xN matrix C. To determine the Nth element of C multiply each element of A by each element of the Nth column of B and sum the results. Helpful information can be found at matrix multiplication. Write a program that reads a 1xN matrix A and an NxN matrix B from input and outputs the 1xN matrix product, C. The first integer input is N, followed by one row of N integers for matrix A and then N rows of N integers for matrix B. N can be of any size >= 2. For coding simplicity, follow each output integer by a space, even the last one. The output ends with a newline. Ex: If the input is: 2 2 3 1 2 3 4 A contains 2 and 3, the first row of B contains 1 and 2, and the second row of B contains 3 and 4. The first element of C is (2 * 1) + (3 * 3), and the second element of C is (2 * 2) + (3 * 4). The program output is: 11 16 Note: Store matrices A and C into one-dimensional arrays and matrix B into a two-dimensional array. Note: The name of the file should be Matrix.java and the name of the class should be Matrix.
Added by Alba A.
Close
Step 1
We will use the Scanner class for this. We will read the size of the matrices, the elements of the 1xN matrix, and the elements of the NxN matrix. Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 64 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
(Algebra: multiply two matrices) Write a function to multiply two matrices. The header of the function is: def multiplyMatrix $(a, b)$ To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in $b$, and the two matrices must have elements of the same or compatible types. Let $c$ be the result of the multiplication. Assume the column size of matrix a is n. Each element $c_{i j}$ is $a_{i 1} \times b_{1 j}+a_{i 2} \times b_{2 j}+\ldots .+a_{i n} \times b_{n j} .$ For example, for two $3 \times 3$ matrices a and b, $c$ is $$\left(\begin{array}{lll} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{array}\right) \times\left(\begin{array}{lll} b_{11} & b_{12} & b_{13} \\ b_{21} & b_{22} & b_{23} \\ b_{31} & b_{32} & b_{33} \end{array}\right)=\left(\begin{array}{lll} c_{11} & c_{12} & c_{13} \\ c_{21} & c_{22} & c_{23} \\ c_{31} & c_{32} & c_{33} \end{array}\right)$$ where $c_{i j}=a_{i 1} \times b_{1 j}+a_{i 2} \times b_{2 j}+a_{i 3} \times b_{3 j}$ Write a test program that prompts the user to enter two $3 \times 3$ matrices and displays their product. Here is a sample run:
Problem 4. If $A=\left(\begin{array}{rr}-3 & 0 \\ 7 & -4\end{array}\right)$ $B=\left(\begin{array}{rr}2 & -1 \\ -7 & 4\end{array}\right)$ and $C=\left(\begin{array}{rr}1 & 0 \\ -2 & -4\end{array}\right)$ find $2 A-3 B+4 C$ For scalar multiplication, each element is multiplied by the scalar quantity, hence $$ \begin{aligned} &2 A=2\left(\begin{array}{rr} -3 & 0 \\ 7 & -4 \end{array}\right)=\left(\begin{array}{rr} -6 & 0 \\ 14 & -8 \end{array}\right) \\ &3 B=3\left(\begin{array}{rr} 2 & -1 \\ -7 & 4 \end{array}\right)=\left(\begin{array}{rr} 6 & -3 \\ -21 & 12 \end{array}\right) \end{aligned} $$ and $4 C=4\left(\begin{array}{rr}1 & 0 \\ -2 & -4\end{array}\right)=\left(\begin{array}{rr}4 & 0 \\ -8 & -16\end{array}\right)$ Hence $2 A-3 B+4 C$ $$ \begin{aligned} &=\left(\begin{array}{rr} -6 & 0 \\ 14 & -8 \end{array}\right)-\left(\begin{array}{rr} 6 & -3 \\ -21 & 12 \end{array}\right)+\left(\begin{array}{rr} 4 & 0 \\ -8 & -16 \end{array}\right) \\ &=\left(\begin{array}{cc} -6-6+4 & 0-(-3)+0 \\ 14-(-21)+(-8) & -8-12+(-16) \end{array}\right) \\ &=\left(\begin{array}{rr} -8 & 3 \\ 27 & -36 \end{array}\right) \end{aligned} $$ When a matrix $A$ is multiplied by another matrix $B$, a single matrix results in which elements are obtained from the sum of the products of the corresponding rows of $A$ and the corresponding columns of $B$. Two matrices $A$ and $B$ may be multiplied together, provided the number of elements in the rows of matrix $A$ are equal to the number of elements in the columns of matrix $B$, In general terms, when multiplying a matrix of dimensions $(m$ by $n$ ) by a matrix of dimensions ( $n$ by $r$ ), the resulting matrix has dimensions ( $m$ by $r$ ). Thus a 2 by 3 matrix multiplied by a 3 by 1 matrix gives a matrix of dimensions 2 by 1 .
Enter the following matrix in MATLAB. A = [-4 -1 -4; -16 -12 -28; 8 8 14] (a) Determine elementary matrices E1, E2, E3 of Type III such that E3E2E1A = U with U an upper triangular matrix. The matrix E1 should turn the element in position (2,1) into a 0. Enter this matrix in MATLAB as E1 using commands similar to the ones in Example 1. The matrix E2 should turn the element in position (3,1) into a zero. Enter this matrix in MATLAB as E2. Note that to zero out the entries in column 1, you need to add or subtract a multiple of row 1. Once you have found the matrices E1 and E2, compute the product E2E1A in MATLAB. Use format rat so that the entries will be given as fractions. Based on the result, determine the matrix E3 that turns the element in position (3,2) into a zero. Enter this matrix as E3 in MATLAB and compute U=E3*E2*E1*A. (b) Compute the product L = E1^-1E2^-1E3^-1. The matrix L is lower triangular with ones on the diagonal. Enter format short and verify that A = LU by computing A - LU in MATLAB. NOTE: From part (a) we have E3E2E1A = U and therefore A = (E3E2E1)^-1U = (E1^-1E2^-1E3^-1)U. Since the elementary matrices and their inverses are lower triangular and have 1’s along the diagonal, as is always true for elementary matrices of the third type, it follows that L is also lower triangular with 1’s along the diagonal.
Sri K.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
Watch the video solution with this free unlock.
EMAIL
PASSWORD