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

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:...
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>;
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?
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.
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
In Java, you can iterate an ArrayList in different ways. Write the following methods to print...
In Java, you can iterate an ArrayList in different ways. Write the following methods to print Integers in an ArrayList iterating in different ways: 1. // Using basic while / for loop void printArrayListBasicLoop(ArrayList<Integer> al); 2. // Using enhanced for loop (:) void printArrayListEnhancedLoop(ArrayList<Integer> al); 3. // Using basic for loop with iterator void printArrayListForLoopListIterator(ArrayList<Integer> al); 4. // Using basic while loop with iterator void printArrayListWhileLoopListIterator(ArrayList<Integer> al);
Show how you multiply 5 3/4 inches by 2.54cm.
Show how you multiply 5 3/4 inches by 2.54cm.
Multiply the following base 10 numbers together and then show how the same result can be...
Multiply the following base 10 numbers together and then show how the same result can be obtained via the DFT (fft). Explain your method. 823415 multiplied by 234671
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT