Question

In: Computer Science

In JAVA How can you multiply two matrices that are different lengths. the answer should be...

In JAVA

How can you multiply two matrices that are different lengths.

the answer should be in 3d array so [][][]

how to multiply a 3d array with a 2d array and the result is in 3d array

for example:

double[][][] nums1 = { { {0.0, 0.1, 2.0}, {3.0,4.0,5.0} },

{ {6.0,7.0,8.0}, {9.0,10.0,11.0} } }

int [][] nums2 = { {1,2,3}, {4,5,6}, {7,8,9}}

and the results should be in [][][] <-- in this dimension

int[][][] answer = { {  {18, 21,24}, {54,66,78} },

{ {90, 11, 132}, {126,156,186} } }

Thanks !

Solutions

Expert Solution

import java.util.Random;


public class MatrixMultiplication {
        
        public static double[][] multiplyMatrix(double a[][], int b[][]) {
                
                if(a[0].length != b.length) {
                        throw new IllegalArgumentException("Matrix size is not allowed for multiplication");
                }
                
                double product[][] = new double[a.length][b[0].length];
                // multiply both matrix
                for (int i = 0; i < a.length; i++) {
                        for (int j = 0; j < b[0].length; j++) {
                                for (int k = 0; k < b.length; k++) {
                                        product[i][j] = product[i][j] + a[i][k] * b[k][j];
                                }
                        }
                }
                
                return product;
        }

        public static void printMatrix(double mat[][]) {
                for (int i = 0; i < mat.length; i++) {
                        for (int j = 0; j < mat[i].length; j++) {
                                System.out.printf("%10.2f", mat[i][j]);
                        }
                        System.out.println();
                }
        }

        public static void main(String[] args) {
                double[][][] nums1 = { { {0.0, 0.1, 2.0}, {3.0,4.0,5.0} },
                                { {6.0,7.0,8.0}, {9.0,10.0,11.0} } };
                int [][] nums2 = { {1,2,3}, {4,5,6}, {7,8,9}};
                
                double [][][] result = new double[nums1.length][][];
                for(int i=0; i<nums1.length; i++) {
                        result[i] = multiplyMatrix(nums1[i], nums2);
                        printMatrix(result[i]);
                        System.out.println();
                }
                
        }
}

**************************************************
So, I have take a 2D matrix from 3D matrix, and then created the array which contains the 2D matrices in a 3D matrix.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

The answer should be in JAVA. You will design and implement two classes to support a...
The answer should be in JAVA. You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game. Read and understand the client program to find out the requirements for the HandShape and Player classes. The rules of the Rock-Paper-Scissors game are:     Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️ Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape....
The answer should be in JAVA. You will design and implement two classes to support a...
The answer should be in JAVA. You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game. Read and understand the client program to find out the requirements for the HandShape and Player classes. The rules of the Rock-Paper-Scissors game are:     Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️ Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape....
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
multiply2nums should accept 2 values and return the product (answer you get when you multiply). greeting...
multiply2nums should accept 2 values and return the product (answer you get when you multiply). greeting should accept one value and print an appropriate greeting using that value. For example, if you sent "Steven" to the greeting function, it should print "Hello, Steven" def main(): user = input("Please enter your name ") greeting(user) try: num1 = int(input("Please enter an integer ")) num2 = int(input("please enter another integer ")) result = multiply2nums(num1,num2) print("The product of your two numbers is ",result) except:...
Should stock exchanges allow listing of shares with different voting rights? can you answer with the...
Should stock exchanges allow listing of shares with different voting rights? can you answer with the detailed explanations please?
Complete the following Java-like pseudocode. Your code should multiply all the numbers in nums and store...
Complete the following Java-like pseudocode. Your code should multiply all the numbers in nums and store it in a variable called mult      public static void main(String[] args){                         int[] nums = <some array values>;
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional...
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional arrays of integers.
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the...
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the specifications for the calculator: The calculator has one “accumulator”, which holds the result of all prior calculations. The calculator is set to 0.0 when the calculator is turned on (i.e., instantiated). On the console, the user then enters an operator (+, -, *, or /) and a number. Your console will then apply the operator and operand to the number in the accumulator, and...
Church organs have a set of pipes with different lengths. With those different pipes organs can...
Church organs have a set of pipes with different lengths. With those different pipes organs can produce sounds over a wide range of frequencies. If the lowest frequency produced by an organ is 31.6 Hz, and the highest frequency is 1.80 kHz, then what is the shortest possible wavelength of sound the organ can produce? Assume that the speed of sound is 336 m/s. B) What is the longest possible sound wavelength the organ can produce?
1.Since all projects are different, how can you determine what should be included in the project...
1.Since all projects are different, how can you determine what should be included in the project concept and project plan? minimum - 150-200 words please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT