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:...
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?
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
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices...
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Discuss the differences between two different cultures and how they communicate. You can focus on business...
Discuss the differences between two different cultures and how they communicate. You can focus on business etiquette/communication between two countries. Maybe look at how different cultures communicate with patients or nonverbal differences in communicating an greetings. Your assignment should be 400-500 words in length. Make sure to cite your sources
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT