[Java:] Summe der Diagonalen einer Matrix berechnen?

topfklao

Christoph N.
ID: 118468
L
20 April 2006
885
17
Hi hi!!!

Ich hänge gerade bei der Berechnung von Diagonalen einer Matrix fest :LOL:...

Ich habe folgende Quatratische Matrix gegeben:

1 2 3
1 2 4
2 1 1


Diese habe ich wie folgt in einen Array geschrieben:

array[0][0] = 1;
array[0][1] = 2;
array[0][2] = 3;

array[1][0] = 2;
array[1][1] = 2;
array[1][2] = 1;

array[2][0] = 3;
array[2][1] = 4;
array[2][2] = 1;


Nun möchte ich die Summen der 5 Diagonalen berechnen:
Heißt von oben hergesehen:

3 => 3
2 + 4 => 6
1 + 2 + 1 => 4
1 + 1 => 2
1 => 1

Wie kann ich das in JAVA machen, sodass das Script auch auf andere quatratische Matrizen anwendbar ist?
 
Das dürfte mit einer Schleife in einer Schleife kein Problem sein. Hab aber im Moment Kopfweh sonst könnt ich diese Gedanken weiter ausführen. Eventuell später! Vielleicht gibt's aber auch schlauere Methoden.

Edit: Nicht zufällig die Regel von Sarrus? ^^
 
Edit: Nicht zufällig die Regel von Sarrus? ^^

Sarrus tuts nur bei 3x3 Matrizen..

Die Schleife.. abgesehen davon, dass ich kein Java kann, kann ich mich an der Schleife versuchen..

Arghl.. ich seh grad, dein Array ist auf jeden Fall falsch.. Du musst das nochmal genauer angeben, due speicherst nämlich im ersten Tripel die erste Zeile, dann die zweite Spalte und dann die dritte Spalte..

Also geh ich mal davon aus, dass du die Spalten gespeichert hast
Code:
 ---
gna, vergiss es
 
Geht ja hier nicht um ein Java-spezifisches Problem.

Überlegung: eine (n x n)-Matrix hat 2n-1 "Diagonalen" (lass das mal keinen Mathematiker hören, weil es eigentlich nur genau eine gibt: die in der Mitte durch).

Wir gehen dabei so vor:
Code:
[FONT=Fixedsys]|---|---|---|---|
| [COLOR=Red]0[/COLOR] |   |   |   |
[/FONT][FONT=Fixedsys]| [COLOR=Gray]0[/COLOR] | [COLOR=SeaGreen]1[/COLOR] | [COLOR=SeaGreen]2[/COLOR] | [COLOR=SeaGreen]3[/COLOR][/FONT][FONT=Fixedsys][COLOR=SeaGreen]\[/COLOR][/FONT][FONT=Fixedsys]|[/FONT]
[FONT=Fixedsys]|---|---|---|---|
| [COLOR=Red]1[/COLOR] |[COLOR=Red]\[/COLOR]  |[COLOR=SeaGreen]\[/COLOR]  |[/FONT][FONT=Fixedsys][COLOR=SeaGreen]\[/COLOR][/FONT][FONT=Fixedsys]  |
[/FONT][FONT=Fixedsys]|  [/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys]| [COLOR=Red]\[/COLOR] | [/FONT][FONT=Fixedsys][COLOR=SeaGreen]\[/COLOR][/FONT][FONT=Fixedsys] | [/FONT][FONT=Fixedsys][COLOR=SeaGreen]\[/COLOR][/FONT][FONT=Fixedsys] |[/FONT]
[FONT=Fixedsys]|---|---|---|---|[/FONT][FONT=Fixedsys]
| [COLOR=Red]2[/COLOR] |[/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys]  |[COLOR=Red]\[/COLOR]  |[/FONT][FONT=Fixedsys][COLOR=SeaGreen]\[/COLOR][/FONT][FONT=Fixedsys]  |
[/FONT][FONT=Fixedsys]|  [/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys]| [/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys] | [COLOR=Red]\[/COLOR] | [/FONT][FONT=Fixedsys][COLOR=SeaGreen]\[/COLOR][/FONT][FONT=Fixedsys] |[/FONT]
[FONT=Fixedsys]|---|---|---|---|
| [COLOR=Red]3[/COLOR] | [/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys] |[/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys]  |[COLOR=Red]\[/COLOR]  |
[/FONT][FONT=Fixedsys]|  [/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys]|  [/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys]| [/FONT][FONT=Fixedsys][COLOR=Red]\[/COLOR][/FONT][FONT=Fixedsys] | [COLOR=Red]\[/COLOR] |
[/FONT][FONT=Fixedsys]|---|---|---|---|[/FONT]
Also im Code
Code:
für(alle v: vertikale; v = [0...n-1]) [COLOR=Red]/* rot */[/COLOR]
{
  summe = 0;
  für(punkt=(v, 0); punkt[sub]y[/sub]<n; punkt[sub]x[/sub]++, punkt[sub]y[/sub]++)
    summe += matrix[punkt[sub]y[/sub]][punkt[sub]x[/sub]];
}

für(alle h: horizontale außer 0; h = [1...n-1]) [COLOR=SeaGreen]/* grün */
[/COLOR]{
  summe = 0;
  für(punkt=(0, h); punkt[sub]x[/sub]<n; punkt[sub]x[/sub]++, punkt[sub]y[/sub]++)
    summe += matrix[punkt[sub]y[/sub]][punkt[sub]x[/sub]];
}
P.S. ich geh davon aus, dass das oben n Abschreibefehler war und die Matrix in der Form matrix[zeile][spalte] vorliegt.