Matrix dan Implementasinya dalam pemrograman JAVA ( PART II )

5. Matrix Skalar adalah matrix yang mengalami proses perkalian dengan 1 angka konstanta.

public class matrixScalar1{
	
	public static void main( String [] args ){
		int value = Integer.parseInt( args[0] );
		int [][] matrix = { {1,2,3}, {1,2,3} };
		processMultiply( value, matrix );
		displayMatrix( matrix );
	}
	
	public static void processMultiply( int value, int [][] matrix){		
		for (int a=0; a<2; a++){
			for(int b=0; b<3; b++){
				matrix[a][b] = matrix[a][b] * value;
			}
		}
	}
	
	public static void displayMatrix( int [][] matrix ){
		for(int a=0; a<2; a++){
			for(int b=0; b<3; b++){
				System.out.print(matrix[a][b] + " ");
			}
			System.out.println();
		}
	}
}

6. Matrix Scalar kedua, yaitu perkalian antar matrix dengan syarat baris x kolom = kolom x baris

import java.util.Scanner;

public class matrixScalar2{
	
	private static Scanner scan;
	
	public static void main( String [] args ){
		int bar1 = Integer.parseInt( args[0] );
		int kol1 = Integer.parseInt( args[1] );
		int bar2 = Integer.parseInt( args[2] );
		int kol2 = Integer.parseInt( args[3] );
		if ( kol1 != bar2 ){
			System.out.println ("Kolom matrix 1 != Baris matrix 2");
		}else{
			scan = new Scanner( System.in );
			System.out.println( "Input Matrix 1" );
			int [][] matrix1 = inputMatrix( bar1, kol1 );
			System.out.println( "Input Matrix 2" );
			int [][] matrix2 = inputMatrix( bar2, kol2 );
			System.out.println ( "Matrix 1:");
			displayMatrix( matrix1, bar1, kol1 );
			System.out.println ( "Matrix 2:");
			displayMatrix( matrix2, bar2, kol2 );
			int [][] result = processMultiply ( matrix1, matrix2, bar1, kol1, kol2 );
			System.out.println ( "Result Matrix Scalar :");
			displayMatrix( result, bar1, kol1 );
		}		
	}
	
	public static int [][] inputMatrix( int bar, int kol ){		
		int[][] matrix = new int[bar][kol];
		for (int a=0; a<bar; a++){
			for(int b=0; b<kol; b++){
				System.out.print("input index ["+a+","+b+"] =");
				matrix[a][b] = scan.nextInt();
			}
		}
		return matrix;
	}
	
	public static int [][] processMultiply( int [][] matrix1, int [][] matrix2, int bar, int kol, int kol2 ){
		int [][] result = new int[bar][kol];
		for ( int a=0; a<bar; a++ ){
			for ( int b=0; b<kol2; b++ ){
				for ( int c=0; c<kol; c++){
					result[a][b] = result[a][b] + matrix1[a][c] * matrix2[c][b];
				}
			}
		}
		return result;
	}
	
	public static void displayMatrix( int [][] matrix, int bar, int kol ){
		for(int a=0; a<bar; a++){
			for(int b=0; b<kol; b++){
				System.out.print(matrix[a][b] + " ");
			}
			System.out.println();
		}
	}
}


7. Matrix Segitiga Bawah adalah matrix yang semua elemen dibawah dialonal utamanya adalah 0

import java.util.Random;
public class matrixSegitigaBawah{	
	public static void main( String [] args ){
		int ordo = Integer.parseInt( args[0] );
		int [][] matrix = createMatrix( ordo );
		displayMatrix( matrix, ordo );
	}
	
	public static int[][] createMatrix( int ordo ){
		int [][] result = new int[ordo][ordo];
		for(int a=0; a<ordo; a++){
			for(int b=0; b<ordo; b++){
				if ( ( b<a ) ){
					result[a][b] = 0;
				}else{
					result[a][b] = new Random().nextInt(9) + 1;
				}
			}
		}
		return result;
	}
	
	public static void displayMatrix( int [][] matrix, int ordo ){
		for(int a=0; a<ordo; a++){
			for(int b=0; b<ordo; b++){
				System.out.print(matrix[a][b] + " ");
			}
			System.out.println();
		}
	}
}


8. Matrix Segitiga Atas adalah matrix yang semua elemen diatas diagonal utamanya adalah nol

import java.util.Random;
public class MatrixSegitigaAtas{
	
	public static void main( String [] args ){
		int ordo = Integer.parseInt( args[0] );
		int [][] matrix = createMatrix( ordo );
		displayMatrix( matrix, ordo );
	}
	
	public static int[][] createMatrix( int ordo ){
		int [][] result = new int[ordo][ordo];
		for(int a=0; a<ordo; a++){
			for(int b=0; b<ordo; b++){
				if ( ( a<b ) ){
					result[a][b] = 0;
				}else{
					result[a][b] = new Random().nextInt(9) + 1;
				}
			}
		}
		return result;
	}
	
	public static void displayMatrix( int [][] matrix, int ordo ){
		for(int a=0; a<ordo; a++){
			for(int b=0; b<ordo; b++){
				System.out.print(matrix[a][b] + " ");
			}
			System.out.println();
		}
	}
}


9. Matrix Transpose adalah matrix yang mana posisi elemen baris ditukar dengan posisi elemen kolom

import java.util.Scanner;
public class MatrixTranspose{
	private static Scanner scan;
	
	public static void main( String [] args ){
		int bar = Integer.parseInt( args[0] );
		int kol = Integer.parseInt( args[1] );

		scan = new Scanner ( System.in );
		
		int [][] initMatrix = createInitMatrix( bar, kol );
		int [][] transposedMatrix = doTranspose( initMatrix, bar, kol );		
		System.out.println ( "Origin Matrix is :" );
		displayMatrix ( initMatrix, bar, kol );
		System.out.println ( "Transposed Matrix is :" );
		displayMatrix ( transposedMatrix, kol, bar );
	}
	
	public static int[][] createInitMatrix ( int bar, int kol ){
		int [][] result = new int [bar][kol];
		for (int a=0; a<bar; a++){
			for ( int b=0; b<kol; b++){
				System.out.print("input index ["+a+","+b+"] =");
				result[a][b] = scan.nextInt();
			}
		}
		return result;
	}
	
	public static int[][] doTranspose ( int[][] matrix, int bar, int kol ){
		int[][] result = new int[kol][bar];
		for ( int a=0; a< bar; a++ ){
			for ( int b=0; b< kol; b++ ){
				result[b][a] = matrix[a][b];
			}
		}
		return result;
	}
	
	public static void displayMatrix( int [][] matrix, int bar, int kol ){
		for(int a=0; a<bar; a++){
			for(int b=0; b<kol; b++){
				System.out.print(matrix[a][b] + " ");
			}
			System.out.println();
		}
	}
	
}


10. Matrix Simetris adalah matrix yang jumlah baris dan kolomnya sama.

public class MatrixSimetris{

	public static void main(String [] args)	{
		int ordo = Integer.valueOf(args[0]);
		int [][] matrix = createMatrixSimetris(ordo);
		displayMatriSimetris( matrix, ordo);
	}
	
	public static int[][] createMatrixSimetris(int ordo){
		int [][] result = new int[ordo][ordo];
		for (int a=0; a<ordo; a++){
			for (int b=0; b<ordo; b++){
				result[a][b] = new Random().nextInt(9)+1;
			}
		}
		return result;
	}
	
	public static void displayMatriSimetris(int [][] matrix, int ordo){
		for (int a=0; a<ordo; a++){
			for (int b=0; b<ordo; b++){
				System.out.print(matrix[a][b]+"  ");
			}
			System.out.println("");
		}
	}
	
}

Tagged: , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: