Java computes the multiplication of two n-order matrices

Source: Internet
Author: User

Oneself is a novice, research Java data structure, see two n-order matrix multiplication of the topic, I tried to write a simple demo, where there is bad place, also hope that we have more suggestions, directly on the code:

Package com.shujujiegou01;

public class Juzhenchengfa {
Computes the multiplication of two n-order matrices
public static void Squaremult (int[][] A, int[][] B, int[][] c, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; J < N; j + +) {
C[I][J] = 0;
for (int k = 0; k < n; k++) {
C[I][J] + = a[i][k] * B[k][j];
}
}
}
}

Print matrix
public static void PrintArray (int a[][]) {
for (int k = 0; k < a.length; k++) {
for (int l = 0; l < a[k].length; l++) {
Print each element and the location of its coordinates
System.out.print (A[k][l] + "-" + "(" + K + "," + L + ")" + ",");
}
System.out.println ();
}
}

Matrix Column Interchange
public static void Swaprowcloumn (int a[][]) {
int num = a.length;

Is for the convenience of debugging, it is so defined

int e[][] = new Int[num][num];
for (int k = 0; k < num; k++) {
for (int l = 0, x = a[k].length; l < x; l++) {
Print each element and the location of its coordinates
E[K][L] = a[l][k];
System.out.print (E[k][l] + "-" + "(" + K + "," + L + ")" + ",");
}
System.out.println ();
}
}

Main function test
public static void Main (string[] args) {
TODO auto-generated Method Stub
int a[][] = new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[][] = new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int c[][] = new INT[3][3];

System.out.println ("Matrix A:");
PrintArray (a);
System.out.println ("A matrix after the interchange of rows and columns:");
Swaprowcloumn (a);
PrintArray (a);
System.out.println ("Matrix B:");
PrintArray (b);
System.out.println ("A matrix after the interchange of rows and columns:");
Swaprowcloumn (b);
PrintArray (b);
Call
Squaremult (A, B, C, 3);
Print
System.out.println ("Matrix C:");
PrintArray (c);

Results of the calculation after the exchange
SYSTEM.OUT.PRINTLN ("Swap rows and columns of Matrix C:");
Swaprowcloumn (c);
}

}

Attach the result of the operation:

Red parts, personal feelings have problems, called the row and column interchange method, it should be calculated that the C matrix is in accordance with the column after the exchange of a matrix and B matrix to calculate, should directly get the answer to the red box; Feel where there is a problem, but can't say!!!

Java computes the multiplication of two n-order matrices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.