Hier ein neues mathematisches Script, um eine zufällig erzeugte Matrix zu transponieren (bzw. zu spiegeln), d.h. sie entlang ihrer Hauptdiagonalen zu spiegeln:
int reihen = 3;
int spalten = 3;
double [][] matrix = new double[reihen][spalten];
double [][] transponiert = new double[reihen][spalten];
//Matrix füllen
for (int i = 0; i < reihen; i++)
{
for(int j = 0; j < spalten; j++)
{
matrix[i][j] = Math.round(Math.random()*1000+1);
}
}
System.out.println("Meine Matrix: ");
// Matrix ausgeben
for (int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < spalten; j++)
{
//Ausgabe mit Tabstopp, sodass es Matirx ähnlich aussieht
System.out.print(matrix[i][j] + "\t");
}
System.out.println("");
}
// Transponieren
for(int i = 0; i < transponiert.length; i++)
{
for(int j = 0; j < matrix[i].length; j++)
{
transponiert[j][i] = matrix[i][j];
}
}
System.out.println("\nTransponierte Matrix: ");
// transponierte Matrix ausgeben
for (int i = 0; i < transponiert.length; i++)
{
for(int j = 0; j < transponiert[i].length; j++)
{
//Ausgabe mit Tabstopp, sodass es Matirx ähnlich aussieht
System.out.print(transponiert[i][j] + "\t");
}
System.out.println("");
}
Keine Kommentare:
Kommentar veröffentlichen