rootUser

transpose a mattrix using method

May 24th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class Trans
  4. {
  5.  
  6.  
  7.     public static double[][] transposeMatrix(double [][] m)
  8.     {
  9.         double[][] temp = new double[m[0].length][m.length];
  10.         for (int i = 0; i < m.length; i++)
  11.             for (int j = 0; j < m[0].length; j++)
  12.                 temp[j][i] = m[i][j];
  13.         return temp;
  14.     }
  15.  
  16.  
  17.  
  18.     public static void main(String args[])
  19.     {
  20.         // initialize the variable
  21.         Scanner s = new Scanner(System.in);
  22.         System.out.println("enter row and column");
  23.  
  24.         int row = s.nextInt();
  25.         int column = s.nextInt();
  26.         double[][] a = new double[row][column];
  27.         for (int i = 0; i < row; i++)
  28.         {
  29.             for (int j = 0; j < column; j++)
  30.             {
  31.                 double b = s.nextDouble();
  32.                 a[i][j]=b;
  33.             }
  34.             System.out.println();
  35.         }
  36.         double[][] c = new double[row][column];
  37.  
  38.         c = transposeMatrix(a);
  39.  
  40.         // operation for transpose
  41.  
  42.         System.out.println("Transpose matrix:");
  43.         // After Transpose the matrix print the result
  44.         for (int i = 0; i < column; i++)
  45.         {
  46.             for (int j = 0; j < row; j++)
  47.             {
  48.                 System.out.print(c[i][j]+" ");
  49.             }
  50.             System.out.println();
  51.         }
  52.     }
  53. }
Add Comment
Please, Sign In to add comment