Share on Google+Share on Google+

nasiee
transpose
2 Answer(s)      5 years and 8 months ago
Posted in : Java Beginners

how to write transpose of a matrix a program....

Ads

View Answers

August 16, 2011 at 1:21 PM


public class Transpose {

public static void main(String[] args) {

    // create N-by-N matrix
    int N = Integer.parseInt(args[0]);
    int[][] a = new int[N][N];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            a[i][j] = N*i + j;
        }
    }

    // print out initial matrix
    System.out.println("Before");
    System.out.println("------");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            System.out.printf("%4d", a[i][j]);
        }
        System.out.println();
    }

    // transpose in-place
    for (int i = 0; i < N; i++) {
        for (int j = i+1; j < N; j++) {
            int temp = a[i][j];
            a[i][j] = a[j][i];
            a[j][i] = temp;
        }
    }

    // print out transposed matrix
    System.out.println();
    System.out.println("After");
    System.out.println("------");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            System.out.printf("%4d", a[i][j]);
        }
        System.out.println();
    }

}

}



August 17, 2011 at 11:41 AM


import java.util.*;

public class Transpose {
        public static void main(String[] args) throws Exception {
                int rows, cols;
                int[][] matrix, tmatrix;
                Scanner input = new Scanner(System.in);
                System.out.print("Enter number of rows: ");
                rows = input.nextInt();
                System.out.print("Enter number of columns: ");
                cols = input.nextInt();
                matrix = new int[rows][cols];
                tmatrix = new int[cols][rows];
                System.out.println("Enter elements for Matrix");
                for (int i = 0; i < rows; i++) {
                        for (int j = 0; j < cols; j++) {
                                matrix[i][j] = input.nextInt();
                        }
                }
                for (int i = 0; i < rows; i++) {
                        for (int j = 0; j < cols; j++) {
                                tmatrix[i][j] = matrix[j][i];
                        }
                }
                System.out.println("Matrix is:");
                for (int i = 0; i < rows; i++) {
                        for (int j = 0; j < cols; j++) {
                                System.out.print(matrix[i][j] + " ");
                        }
                        System.out.println();
                }
                System.out.println("Its transpose is: ");
                for (int i = 0; i < cols; i++) {
                        for (int j = 0; j < rows; j++) {
                                System.out.print(tmatrix[i][j] + " ");
                        }
                        System.out.println();
                }
        }
}









Related Tutorials/Questions & Answers:
transpose
transpose  how to write transpose of a matrix a program
transpose of matrix
transpose of matrix  write a program in java to declare a square matrices 'A' or order n which is less than 20.allow in user to input only positive integers into the matrix and print the transpose
Advertisements
transpose matrix
transpose matrix  write a program in java to declare a square matrices 'A' or order n which is less than 20.allow in user to input only positive integers into the matrix and print the transpose of it. for this program u r given
Java Transpose of a matrix
Java Transpose of a matrix In this section, you will learn how to determine the transpose of a matrix.The transpose of a matrix is formed by interchanging... of the transposed matrix. The transpose of A is denoted by AT. Through the following
using array
using array  transpose a matrix
using array
using array  transpose a matrix
java applet
transpose. The web page should have two text boxes and a submit button labbeled... with the submit button labelled as Compute Transpose. When the Compute Transpose Button clicked , the transpose of the input matrix has to be displayed
java applet
transpose. The web page should have two text boxes and a submit button labbeled... with the submit button labelled as Compute Transpose. When the Compute Transpose Button clicked , the transpose of the input matrix has to be displayed
java programs
java programs  A union B, transpose of matric, denomination..., Transpose of matrix: import java.util.*; public class Transpose { public...(); } System.out.println("Its transpose
find eror inc++
) { cout<<"Matrix not square so Transpose not possible!"; } else...;<endl; } cout<<endl<<"Matrix Transpose: "<<endl<<
Java
] ) { // transpose the numbers inprocess = data[b]; data[b] = data[b+1]; data[b+1
Need help - Java Beginners
array as its argument, performs its transpose, and returns the transposed n * m...(); } } public int[][] transpose(int[][] arr) { int[][] temp = arr
Java
] ) { // transpose the numbers inprocess = data[b]; data[b] = data[b+1]; data
finding the eigenvalues and eigenvector of n*n matrix in java
[][] {{4,3,1,2},{1,1,1,1},{1,2,1,1},{1,1,1,1} }; double t, c, s; int p, q, icount..., rowOfElMax, maxElRow); q = rowOfElMax[0]; p = colRowOfElMax[q]; icount = 0; state = 1; // Iterations while
programes on array
iv) find transpose of an matrix   1) import java.util.*; class...;Transpose of Matrix
questions
iv) find transpose of an matrix Q. 7 programes on strings...questions   Q. 1 programes on if....else 1. write a program... of a student out of 100 and print grades using else .... if ladder Q. 2
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads