Advertisement
Cieslin

PWMatrixTransposite_Multiply

Mar 12th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. //////////////////////////////////////////////////KLASA MACIERZ////////////////////////////////////////////////////
  2. package com.cieslin;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Macierz {
  7.     Scanner s = new Scanner(System.in);
  8.     private double[][] Tab;
  9.  
  10.     public double[][] createMatrix(int w, int k) {
  11.         Tab = new double[w][k];
  12.         for (int i = 0; i < w; i++)
  13.             for (int j = 0; j < k; j++) {
  14.                 System.out.print("[" + (i + 1) + "][" + (j + 1) + "]:");
  15.                 Tab[i][j] = s.nextDouble();
  16.             }
  17.         return Tab;
  18.     }
  19.  
  20.     public double[][] multiplyMatrix(double[][] TabA, double[][] TabB) {
  21.         double suma = 0;
  22.         double[][] Tab = new double[TabA.length][TabB[0].length];
  23.         for (int i = 0; i < TabA.length; i++)
  24.             for (int j = 0; j < TabB[0].length; j++) {
  25.                 suma = 0;
  26.                 for (int k = 0; k < TabA[0].length; k++)
  27.                     suma += TabA[i][k] * TabB[k][j];
  28.                 Tab[i][j] = suma;
  29.             }
  30.         return Tab;
  31.     }
  32.  
  33.     public double[][] transpositeMatrix(double[][] Tab) {
  34.         double[][] TabT = new double[Tab[0].length][Tab.length];
  35.         for (int i = 0; i < Tab[0].length; i++)
  36.             for (int j = 0; j < Tab.length; j++) {
  37.                 TabT[i][j] = Tab[j][i];
  38.             }
  39.         return TabT;
  40.     }
  41.  
  42.     public void showMatrix(double[][] Tab) {
  43.         for (int i = 0; i < Tab.length; i++) {
  44.             for (int j = 0; j < Tab[0].length; j++)
  45.                 System.out.print(Tab[i][j] + "\t");
  46.             System.out.println("");
  47.         }
  48.     }
  49. }
  50.  
  51. ////////////////////////////////////////////////////////////////MAIN/////////////////////////////////////////////
  52. package com.cieslin;
  53.  
  54. public class Main {
  55.  
  56.     public static void main(String[] args) {
  57.         Macierz m = new Macierz();
  58.         double[][] Tab = m.multiplyMatrix(m.createMatrix(3, 2), m.createMatrix(2, 2));
  59.         m.showMatrix(Tab);
  60.         m.showMatrix(m.transpositeMatrix(Tab));
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement