Matrizenmultiplikation

Dolphon

Erfahrenes Mitglied
Hi,

ich bin gerade dabei ein Programm für die Matrizenmultiplikation zuschreiben.
Allerdings habe ich ein Problem bei der Berechnung von MatrixC.
Der erste Wert ist noch richtig. Alle weiteren sind falsch.
Vllt. kann mir hier jemand helfen.

Hier der Code:



Code:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    int as,az,bs,bz;
    const int max = 2;
    double matrixa [max][max];
    double matrixb [max][max];
    double matrixc [max][max];
    
//Dimensionseingabe

cout<<"Bitte geben sie die Dimension az x as der Matrix A ein.\n\n az: ";
cin>>az;
cout<<"\n as: ";
cin>>as;
cout<<"\n Bitte geben sie die Dimension bz x bs der Matrix B ein.\n bz: ";
cin>>bz;

if (as!=bz)
{
    cout<<"\nSpalten von A muessen dieselbe Anzahl wie die Zeilen von B sein!Fehler\n";
}

cout<<"\n bs: ";
cin>>bs;

//Einlesen von Matrix A

cout << "\n Bitte geben Sie nun die Werte fuer Matrix A ein " << endl;
   for (int i = 0; i < az; i++)
    {    
        cout << "Zeile " << i+1 <<"=" ;
        for (int j = 0; j < as; j++)
        cin >> matrixa[i][j];
    }

// Matrize A zur kontrolle ausgeben
    for (int i = 0; i < az; i++)
    {    
        cout << "\n";
        for (int j = 0; j < as; j++)
        cout << " " << matrixa[i][j];              
    }


// Einlesen von Matrix B

    cout << "\n Bitte geben Sie nun die Werte fuer Matrix B ein " << endl;

    for (int i = 0; i < bz; i++)
    {    
        cout << "Zeile " << i+1 <<"=" ;
        for (int j = 0; j < bs; j++)
        cin >> matrixb[i][j];
    }

// Matrize B zur kontrolle ausgeben
    for (int i = 0; i < bz; i++)
    {    
        cout << "\n";
        for (int j = 0; j < bs; j++)
        cout << " " << matrixb[i][j];        
    }  

//Matrix C berechnen

   for(int f=0; f < az; f++)
     for(int g=0; g < bs; g++)
         //matrixc[f][g] += matrixa[f][g] * matrixb[f][g];  
         matrixc[f][g] = ((matrixa[f][g]*matrixb[f][g])+(matrixa[f][g+1]*matrixb[f+1][g]));



// Matrix C ausgeben

   cout << "\n Das Ergebnis der Multipilkation ist: " << endl;
     for (int i = 0; i < az; i++)
    {    
        cout << "\n";
        for (int j = 0; j < bs; j++)
        cout << " " << matrixc[i][j];        
    }  

    
    
    getch();
    return 0;
}
 
Hallo, dir sollte klar sein das dein Algorithmus die Grenze deines Arrays verletzt.
Lese doch einfach nach wie man Matrizen multipliziert. Ich nehme an wenn du das kapiert hast solltest du auch den Algorithmus hinbekommen.

Stichwort: Falksches Schema
Tipp: Mit 3 for schleifen sollte es einfach sein ;)

mfg
 
Vielen Dank für deine Antwort.

Die Multiplikation klappt nun. Allerdings nur wenn ich den Arrays feste Werte ebe, und nicht manuel eintrage.

Code:
    for(int i = 0; i < bs; i++)
	for(int f=0; f < bz; f++)
     for(int g=0; g < az; g++)
         matrixc[f][i] += matrixa[f][g] * matrixb[g][i];

dir sollte klar sein das dein Algorithmus die Grenze deines Arrays verletzt.

Könntest du das näher erlautern, damit ich den Fehler besser ausmertzen kann.
 
Jetzt sieht dein Algorithmus gut aus, wieso funktioniert er nur bei manuellen Werten ? Das ergibt doch keinen Sinn.

Bei der Matrixmultiplikation gilt die Bedingung: Spaltenanzahl von Matrix A muss identisch mit Zeilenanzahl der Matrix B sein.
Deswegen muss bei deiner innerste for-schleife g < as hin.

Hinweise: Bei deinem ersten Code definierst du eine 2*2 Matrix (max=2) aber fragst wieviele Diemensionen die Matrix haben soll ...?
Entscheidest du dich für die Wahl der Dimension, so wäre es sinnvoll die Matrizen dynamisch zu erstellen da du die Zeilen- und Spaltenanzahl nicht kennst.
C++:
// allocate memory
    _arr = new int*[_row];
    for (int i = 0; i < _row; i++)          // rows gets columns
    {
        _arr[i] = new int[_col];
    }

@Grenzen: das war auf deinen alten Algorithmus bezogen, da du g+1 usw verwendet hast bei der Bedingung g < bs dann wäre im letzten Schritt g = bs -> Speicherverletzung da nicht reserviert.


mfg:)
 
Zurück