The Artima Developer Community
Sponsored Link

Java Answers Forum
Transpose matrix

5 replies on 1 page. Most recent reply: Nov 14, 2005 10:29 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
jimbo fisher

Posts: 4
Nickname: r0punk
Registered: Oct, 2005

Transpose matrix Posted: Nov 11, 2005 5:27 PM
Reply to this message Reply
Advertisement
My program is supposed to read in a multidimensional array(matrix) and print the transposed matrix of it.

I have this code that i wrote and it works fine.. as long as i have the same number of rows as columns.

I am wondering if anyone can help me fix the code so that it will work even if the number of rows/columns are different.

 
import java.util.Scanner;
 
 
public class Project7B2
{
	public static void main(String[] args)
	{
		//declare all variables/arrays
		int rows, columns, tmp;
		int[][] array, matrix;	
		
		//set up the scanner and get the number of rows and columns in the array
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter number of rows: ");
		rows = scan.nextInt();
		System.out.print("Enter number of columns: ");
		columns = scan.nextInt();
		
		//set the size 
		array = new int[rows][columns];
		
		for(int j = 0; j < rows; j++)
		{	
			System.out.println("Enter " + columns + " elements for row " + (j + 1) + ":");
			for(int k = 0; k < columns; k++)
			{	
				array[j][k] = scan.nextInt();
			}
		}		
 
 
		matrix = new int[columns][rows];
 		matrix = array;
 		for(int h = 0; h < rows; h++)
   	{	
			for(int i = 0; i < columns; i++)
     		{
				if(h<i) 
				{
      			tmp = matrix[h][i];
      			matrix[h][i] = matrix[i][h];
      			matrix[i][h] = tmp;
      		}
			}
		}
		System.out.println("The tranpose matrix is ->");
				
		for(int l = 0; l < columns; l++)
		{	
			for(int m = 0; m < rows; m++)
			{
				System.out.print(matrix[l][m] + "\t");	
			}
			System.out.println();
		}		
	}
}


When their are different size columns/rows i get outofbounds error. > < any help would be greatly appreciated.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Transpose matrix Posted: Nov 14, 2005 7:38 AM
Reply to this message Reply
Well, you described the error yourself: You have a different number of colums and rows.


Example: M = new int [4][8]

You cannot access M [7][3], because you only have 4 elements.


In your example you create M = new int [columns][rows]

But you access it like M[rowIndex][columnIndex].


Instead of exchanging 2 fields in the original matrix, create a new matrix and put the values directly in there reading the old matrix.


Also you made the error of assigning
matrix = array;
This way you don't copy the values, but accessing matrix you also change array.


Therefore:

array = new int[rows][columns];
//fill with values
matrix = new int[columns][rows];
for (int iR = 0; iR < rows; iR++) {
for (int iC = 0; iC > columns; iC++) {
matrix[iC][iR] = array[iR][iC];
}
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Transpose matrix Posted: Nov 14, 2005 7:41 AM
Reply to this message Reply
Well, sometimes it even happens to me that I use the wrong tag.
Here is the formatted code:
    array = new int[rows][columns];
    //fill with values
    matrix = new int[columns][rows];
    for (int iR = 0; iR < rows; iR++) {
        for (int iC = 0; iC > columns; iC++) {
            matrix[iC][iR] = array[iR][iC];
        }
    }

Jody Brown

Posts: 8
Nickname: jody
Registered: Mar, 2002

Re: Transpose matrix Posted: Nov 14, 2005 8:31 AM
Reply to this message Reply
Is this how you want it to work? I have made a few codechanges/assumptions on what (I think) you wanted.

import java.util.Scanner;
  
public class Project7B2
{
	public static void main(String[] args)
	{
		//declare all variables/arrays
		int idx1, idx2;
		int[][] array, matrix;	
 
		//set up the scanner and get the number of rows and columns in the array
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter number of rows: ");
		idx1 = scan.nextInt();
		System.out.print("Enter number of columns: ");
		idx2 = scan.nextInt();
		
		//set the size 
		array = new int[idx1][idx2];
		matrix = new int[idx2][idx1];
		
		for(int j = 0; j < idx1; j++)
		{	
			System.out.println("Enter " + idx2 + " elements for row " + (j + 1) + ":");
			for(int k = 0; k < idx2; k++)
			{	
				array[j][k] = scan.nextInt();
			}
		}		
 
 
		
 		//matrix = array;
 		for(int h = 0; h < idx1; h++)
 		{	
			for(int i = 0; i < idx2; i++)
     		{
				matrix[i][h] = array[h][i];
			}
		}
 		
		System.out.println("The original matrix is ->");
		
		for(int l = 0; l < idx1; l++)
		{	
			for(int m = 0; m < idx2; m++)
			{
				System.out.print("[" + array[l][m] + "]");	
			}
			System.out.println();
		}		
		System.out.println("The tranpose matrix is ->");
				
		for(int l = 0; l < idx2; l++)
		{	
			for(int m = 0; m < idx1; m++)
			{
				System.out.print("[" + matrix[l][m] + "]");	
			}
			System.out.println();
		}		
	}
}

Jody Brown

Posts: 8
Nickname: jody
Registered: Mar, 2002

Re: Transpose matrix Posted: Nov 14, 2005 8:34 AM
Reply to this message Reply
I didn't see your post before I posted my solution, Matthias. I think my solution works along the same lines as you outlined however.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Transpose matrix Posted: Nov 14, 2005 10:29 PM
Reply to this message Reply
As long as no one get's hurt ... ;)

Flat View: This topic has 5 replies on 1 page
Topic: java 3d projections Previous Topic   Next Topic Topic: HowTo: Display Labels after an Application is Loaded.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use