DEV Community

Neelakandan R
Neelakandan R

Posted on

2 1 1 1 1

matrix multipilication,matrix addision

matrix multipilication:

package afterfeb13;

public class matrixmulti {
    public static void main(String[] args) {
        int[][] a = { { 1, 3, 5 }, { 5, 6, 7 }, { 1, 2, 3 } };
        int[][] b = { { 1, 8, 5 }, { 5, 9, 7 }, { 1, 7, 3 } };
        int[][] c = new int[a.length][a[0].length];
        for (int row = 0; row < a.length; row++) {
            for (int col = 0; col < b[0].length; col++)

            {
                for (int k = 0; k < b.length; k++) {
                    c[row][col] += a[row][k] * b[k][col];// c[0][0] = (a[0][0] * b[0][0]) + (a[0][1] * b[1][0]) +
                                                            // (a[0][2] * b[2][0])
//=(1*1)+(2*5)+(5*1)=21//(1*8)+(2*9)+(5*7)=70//(1*5)+(2*7)+(5*3)=21//it represent row *col
                }
                System.out.print(c[row][col] + " ");
            }
            System.out.println();
        }

    }

}

Enter fullscreen mode Exit fullscreen mode

Output:

21 70 41
42 143 88
14 47 28

Reference:https://www.geeksforgeeks.org/java-program-to-multiply-two-matrices-of-any-size/

matrix addision:

package afterfeb13;

public class matrixadd {
    public static void main(String[] args) {
        int[][] a = { { 1, 3, 5 }, { 5, 6, 7 }, { 1, 2, 3 } };
        int[][] b = { { 1, 8, 5 }, { 5, 9, 7 }, { 1, 7, 3 } };
        int[][] c = new int[a.length][a[0].length];// temp con

        for (int row = 0; row < a.length; row++) {
            for (int col = 0; col < b[0].length; col++)

            {

                c[row][col] = a[row][col] + b[row][col];
                System.out.print(c[row][col] + " ");
            }
            System.out.println();

        }

    }

}
Enter fullscreen mode Exit fullscreen mode

Output:
2 11 10
10 15 14
2 9 6

Reference:https://www.geeksforgeeks.org/java-program-to-add-two-matrices/

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (1)

Collapse
 
saravanan_477814b61087a66 profile image
Saravanan

good work bro

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay