import java.util.Random;
public class Matrix {
    Random random =new Random();
    //return a random mXn matrix
    //with value 0 and 1
    public int[][] random(int m,int n){
        int[][] c=new int[m][n];
        for(int i=0;i < m; i++){
            for(int j=0; j  < n; j++){
                c[i][j]=random.nextInt(100);
            }
        }
        return c;
    }
    public int[][] multiply(int[][] a,int[][] b){
        int mA=a.length;
        int nA=a[0].length;
        int mB=b.length;
        int nB=b[0].length;
        if (nA != mB) {
            throw new RuntimeException("Illegal matrix dimentions");

        }
        int [][] c=new int[mA][nB];
        for(int i=0;i < mA;i++){
            for(int j=0; j < nB;j++){
                for(int k=0;k < nA;k++){
                    c[i][j] += a[i][k]*b[k][j];
                }
            }

        }
        return c;
    }
    public int[][] add(int[][] a,int[][] b){
        int n=a[0].length;
        int m=a.length;
        int[][] c=new int[m][n];
        for(int i=0;i < m; i++){
            for(int j=0; j < n;j++){
                c[i][j]=a[i][j]+b[i][j];
            }
        }
        return c;
    }
    //return c=a-b
    public int[][] subtract(int[][] a,int[][] b) {
        int n=a[0].length;
        int m=a.length;
        int[][] c=new int[m][n];
        for(int i=0;i < m; i++){
            for(int j=0; j < n;j++){
                c[i][j]=a[i][j]-b[i][j];
            }
        }
        return c;


    }
    public void print(int x[][]){
        for(int row=0;row < x.length; row++){
            for(int col=0;col < x[row].length;col++){
                System.out.println(x[row][col]+"\t");
            }
            System.out.println();
        }

    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Matrix matrix=new Matrix();
        int[][] a=matrix.random(5, 5);
        int[][] b=matrix.random(5, 5);
        int[][] multiply=matrix.multiply(a, b);
        System.out.println("Result # 1 : ");
        matrix.print(multiply);
        int[][] add=matrix.add(a, b);
        System.out.println("\nResult # 2: ");
        matrix.print(add);
        int[][] subtract=matrix.subtract(a, b);
        System.out.println("\nResult # 3: ");
        matrix.print(subtract);

    }

}
output:
Result # 1 : 
13189   
9159    
8875    
11899   
3438    

19819   
19661   
19152   
16933   
8416    

13133   
14541   
15198   
12551   
6138    

21398   
21419   
19912   
19364   
9247    

20097   
18790   
19236   
16947   
6779    


Result # 2: 
29  
133 
106 
60  
104 

174 
41  
125 
152 
88  

145 
57  
29  
112 
96  

168 
73  
173 
99  
98  

119 
142 
97  
158 
109 


Result # 3: 
-25 
25  
-32 
-42 
14  

0   
-13 
29  
32  
58  

25  
-13 
5   
-8  
34  

4   
-49 
25  
73  
82  

7   
-30 
-33 
-2  
77  

Bazlur Rahman Rokon vai please help me!

asked 06 Oct, 06:11

Mohammad%20Noman's gravatar image

Mohammad Noman
214

edited 07 Oct, 18:41


enter code here
এই প্রোগ্রামে if (nA != mB) { throw new RuntimeException("Illegal matrix dimentions");

}

এই কোডটির কাজ কী?

permanent link

answered 13 Nov, 09:41

Mohammad%20Noman's gravatar image

Mohammad Noman
214

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×198
×194

question asked: 06 Oct, 06:11

question was seen: 141 times

last updated: 13 Nov, 09:41