MatrizenMultiplikation

Lilli

Grünschnabel
Hi,

ich bräuchte mal eure Hilfe.

Wie programmiert man denn eine Matrix-Multiplikation zweier Matrizen, die bereits gegeben sind. Könntet ihr mir das für zwei einfache Matrizen A(4x3) und B(3x4) posten**** Am besten mit einer matrixToString Methode..
Und kann man das auch so programmieren, dass Java dann die zwei gegebenen Matrizen einliest und zusammen mit dem Ergebnis ausgibt, wenn es durchläuft?

Bin noch kompletter Neuling, wie man merkt :rolleyes:

Danke schon mal ;)
 
Ja, aber ein Neuling will ja lernen.
Du willst eine komplette Aufgabe gelöst haben?!

Sollen wir es besser in das Jobforum verschieben oder willst du es selbst versuchen und zeigst deine Anfänge und erklärst woran es scheitert.
 
Hoi, ich habe die gleiche "Frage" wie Lilly (ich schätze wir sind Kommilitonen^^) Nach sehr sehr sehr viel gefluche bin ich inzwischen soweit, das ich folgendes Programm zusammengebastelt habe. Ich bekomme nur immer nen Array out of boundries Fehler. Sieht einer von euch zufällig den meinen Fehler?
Java:
public class Ubung5_GruppeXXX_Aufgabe3{
     public static void main(String[] args)  {

   

    double a[][] = {{4,1,2,0},{2,3,5,0},{3,8,4,1}};

   

    double b[][] = {{-3,2,0},{2,-1,0},{1,-2,1},{-5,3,-7}};

    
 

   double c[][] = multiMatrix(a, b);

    

  

   for (int i=0; i<a.length; i++) {

   for (int j=0; j<a[0].length; j++)

   

       System.out.print(" " + c[i][j] + ",");

       System.out.println();

    }

   

   }  



  public static double[][] xMatrix(double[][] a, double[][] b) {

    

    double[][] c = new double[a.length][a[0].length];

       

      for (int i = 0; i < a.length; i++)

       {

       for (int j = 0; j < a[i].length; j++)

       {

       c[i][j] = a[i][j] * b[i][j];



    }

    }

     {

 return c;

     }

    }

}

Wär super nice wenn ihr mir helfen könntet :)

LG
Quetschkopf
 
Hi,

c hat die Ausmße von a, b ist 4x3 und nicht 3x4, also muss auch i und j ungedeht werden!
Java:
public class Ubung5_GruppeXXX_Aufgabe3 {

    public static void main(String[] args)  {
	 	double a[][] = {{4, 1, 2, 0}, {2, 3, 5, 0}, {3, 8, 4, 1}};
	 	double b[][] = {{-3, 2, 0}, {2, -1, 0}, {1, -2, 1}, {-5, 3, -7}};
 		double c[][] = multiMatrix(a, b);
 		for(int i=0; i<a.length; i++){
 			for(int j=0; j<a[0].length; j++)
 				System.out.print(" " + c[i][j] + ",");
 			System.out.println();
 		}  
	}
 
 	public static double[][] multiMatrix(double[][] a, double[][] b) {
 		double[][] c = new double[a.length][a[0].length];
		for(int i=0; i<a.length; i++)
			for(int j=0; j<a.length; j++)
				c[i][j] = a[i][j] * b[j][i]; // b[j][i] statt b[i][j]
 		return c; 
    }
 
}
 

Neue Beiträge

Zurück