Total members 11895 |It is currently Tue Dec 03, 2024 6:20 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





The matrix mutliplication of any size to any size.We are using here thread of java for multiplication.

How to work :
User enter the number of rows and columns in matrix A and matrix B.
The number of columns in matrix A must equal the number of rows in matrix B.
Then,
The user enter the elements of matrix A.
The user enter the elements of matrix B.
Code:
/*
 * Main.java
 *
 * Created on November 6, 2006, 6:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */



import java.util.Scanner;

/**
 *
 
 */
public class Main {
    
    
/** Creates a new instance of Main */
    public Main() 
    
{
        Scanner cin=new Scanner(System.in);
        System.out.println("Enter the number of rows in matrix A : ");
        A_row=cin.nextInt();
        System.out.println("Enter the number of columns in matrix A : ");
        A_col=cin.nextInt();
        System.out.println("Enter the number of rows in matrix B : ");
        B_row= cin.nextInt();
        System.out.println("Enter the number of columns in matrix B : ");
        B_col=cin.nextInt();
        
        if
(A_col==B_row)
        {
            matrix_A=new int[A_row][A_col];
            matrix_B=new int[B_row][B_col];
            mult_ans=new int[A_row][B_col];
          System.out.println("Enter the elements of matrix A : ");
             for(int i=0;i<A_row;i++)
                 for(int j=0;j<A_col;j++)
                 {
                    matrix_A[i][j]=cin.nextInt();
                 }
          System.out.println("Enter the elements of matrix B : ");
            for(int i=0;i<B_row;i++)
                 for(int j=0;j<B_col;j++)
                 {
                    matrix_B[i][j]=cin.nextInt();  
                 
}
             thread_pool=new mythread[A_row];
             
            for
(int i=0;i<A_row;i++)
            {
                thread_pool[i]=new mythread(i);
                thread_pool[i].start();
            }
           System.out.println("The answer of the matrix A*B : "); 
             printer_thread
=new threadprint[3];

                printer_thread[2]=new threadprint(mult_ans);
                printer_thread[2].start();

}
        else
        
{
           System.out.print("!!! Can't multiply this matrixes !!! "); 
        
}
         
    
}
    
    
/**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

new Main();

// TODO code application logic here
    }

private class mythread extends Thread
    
{
        int index;
         
         mythread
(int index)
         {
             this.index=index;
         }
        public void run() 
        
{
            for(int i=0;i<B_col;i++)
            {
                for(int j=0;j<B_col;j++)
                {
                    mult_ans[index][i]+=matrix_A[index][j]*matrix_B[j][i];
                }
            }
        }
    }
    private class threadprint extends Thread
    
{
        threadprint (int[][] matrix)
        {
            this.matrix=matrix;
        }
        public synchronized  void run()
        {
            for(int i=0;i<matrix.length;i++)
            {
                System.out.print("| ");
                for(int j=0;j<matrix[0].length;j++)
                {
                  System.out.print(matrix[i][j]+"  ");
                }
                System.out.println(" |");
            }
        }
        int[][] matrix;
    }
            
    private int A_row
;
    private int A_col;
    private int B_row;
    private int B_col;
    private int[][] matrix_A;
    private int[][] matrix_B;
    private int[][] mult_ans;
    private mythread[] thread_pool;
    private threadprint[] printer_thread;
}
 





Attachments:
File comment: Matrix multiplay netBeans Project
Matrix_multiplication.rar [12.21 KiB]
Downloaded 2800 times

_________________
Recommend my post if you found it helpful.
Author:
Newbie
User avatar Posts: 15
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Matrix Multiplication using java
 Matrix multiplication     -  
 Matrix multiplication (Product) using C++     -  
 create Sparse Matrix in java     -  
 Perform Multiplication by Shift Left Operations     -  
 Matrix Operations     -  
 How to do a array matrix program     -  
 print element in 2d matrix     -  
 Sparse matrix using a class     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 need help in java     -  



Topic Tags

Java Arrays






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com