Advertisement
Guest User

MultiplicacionMatrices

a guest
Jul 4th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package test;
  7.  
  8. import java.util.Random;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author eliseo
  14.  */
  15. public class Test {
  16.  
  17.     public static int dimension;
  18.     public static int[][] matriz1;
  19.     public static int[][] matriz2;
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) {
  25.         //Imprimir();
  26.         //sumaDeMatrices();
  27.         MultiplicacionDeMatrices();
  28.     }
  29.  
  30.     public static void Imprimir() {
  31.         int arreglo[] = new int[10000];             // el entero arreglo, tendrá diez mil casillas.
  32.         for (int i = 0; i < 10000; i++) {        // Se declara la variabe i en 0 ; mientras i sea menor a diez mil ; aumento de i en 1
  33.             arreglo[i] = (i + 1);               // arreglo[] posee diez mil casillas, se le asigna i, como pate del total de las casillas. Como [i] parte en 0, se le suma 1 para partir desde el 1 y no e 0.
  34.             System.out.println(arreglo[i]);
  35.         }
  36.     }
  37.  
  38.     public static void sumaDeMatrices() {
  39.  
  40.         int matriz1[][] = new int[2][2];
  41.         int matriz2[][] = new int[2][2];
  42.         int matriz3[][] = new int[2][2];
  43.         matriz1[0][0] = 1;
  44.         matriz1[0][1] = 2;
  45.         matriz1[1][0] = 3;
  46.         matriz1[1][1] = 4;
  47.         matriz2[0][0] = 1;
  48.         matriz2[0][1] = 0;
  49.         matriz2[1][0] = 0;
  50.         matriz2[1][1] = 1;
  51.  
  52.         for (int i = 0; i < 2; i++) {
  53.  
  54.             for (int j = 0; j < 2; j++) {
  55.                 for (int k = 0; k < 1; k++) {
  56.                     matriz3[i][j] = (matriz1[i][j] + matriz2[i][j]);
  57.                     System.out.print(" " + matriz3[i][j]);            //Imprime matriz, en orden de fila y columnas
  58.  
  59.                 }
  60.             }
  61.  
  62.             System.out.println("");                             //Salto de espacio
  63.         }
  64.     }
  65.  
  66.     public static void MultiplicacionDeMatrices() {
  67.         PreguntarDimension();
  68.  
  69.         //generar matrices de manera random
  70.         matriz1 = GenerarMatriz();
  71.         matriz2 = GenerarMatriz();
  72.  
  73.         //mostrar las marices generadas
  74.         System.out.println("Matriz 1:");
  75.         ImprimirMatriz(matriz1);
  76.         System.out.println("Matriz 2:");
  77.         ImprimirMatriz(matriz2);
  78.         System.out.println("Multiplicacion:");
  79.  
  80.         //multiplicar las matrices
  81.         int[][] matriz_resultado = MultiplicarMatrices(matriz1, matriz2);
  82.  
  83.         //mostrar el resultado
  84.         ImprimirMatriz(matriz_resultado);
  85.     }
  86.  
  87.     private static void PreguntarDimension() {
  88.         Scanner en = new Scanner(System.in);
  89.         System.out.print("Ingrese dimensión: ");
  90.         dimension = en.nextInt();
  91.         System.out.println("");
  92.     }
  93.  
  94.     private static int[][] GenerarMatriz() {
  95.         int[][] m = new int[dimension][dimension];
  96.         for (int i = 0; i < dimension; i++) {
  97.             for (int j = 0; j < dimension; j++) {
  98.                 m[i][j] = randInt(0, 9);
  99.             }
  100.         }
  101.         return m;
  102.     }
  103.  
  104.     //http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java
  105.     public static int randInt(int min, int max) {
  106.  
  107.         // NOTE: Usually this should be a field rather than a method
  108.         // variable so that it is not re-seeded every call.
  109.         Random rand = new Random();
  110.  
  111.         // nextInt is normally exclusive of the top value,
  112.         // so add 1 to make it inclusive
  113.         int randomNum = rand.nextInt((max - min) + 1) + min;
  114.  
  115.         return randomNum;
  116.     }
  117.  
  118.     private static int[][] MultiplicarMatrices(int[][] matriz1, int[][] matriz2) {
  119.         int[][] r = new int[dimension][dimension];
  120.         //columnas
  121.  
  122.         for (int i = 0; i < dimension; i++) {
  123.             for (int j = 0; j < dimension; j++) {
  124.  
  125.                 int suma = 0;
  126.                 for (int k = 0; k < dimension; k++) {
  127.                     suma += matriz1[j][k] * matriz2[k][i];
  128.                 }
  129.  
  130.                 r[j][i] = suma;
  131.             }
  132.         }
  133.  
  134.         return r;
  135.     }
  136.  
  137.     private static void ImprimirMatriz(int[][] matriz) {
  138.         for (int i = 0; i < dimension; i++) {
  139.             System.out.print("[");
  140.             for (int j = 0; j < dimension; j++) {
  141.                 System.out.print(matriz[i][j] + "\t");
  142.             }
  143.             System.out.println("]");
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement