![]() |
Matrizen-Multiplikation
Hiho, ich bins wieder. Steck mal wieder total fest, da das Program nicht das tut, was es soll. Diesesmal geht es um Matrizenmultiplikation mit 3 Stringgrids und 2D-Arrays.
Bekomme wieder die Fehlermeldung, das " kein geeigneter Gleitkommawert ist. Warscheinlich wieder irgendein Anfängerfehler. *Sich schon mal selbst die hand an den kopf hau*
Delphi-Quellcode:
Schon mal danke im Voraus für eure Hilfe.
procedure TForm3.Matrixmul(Sender:TObject);
var matrixA: array of array of double; matrixB: array of array of double; matrixC: array of array of double; a,b,c,i,j,k:integer; begin a:=StrToInt(EditA.Text); b:=StrToInt(EditB.Text); c:=StrToInt(Editb.Text); SetLength(matrixA,a,b); SetLength(matrixB,b,c); SetLength(matrixc,a,c); For i:= 0 to a-1 do for j:= 0 to b-1 do begin matrixA[i,j]:=StrToFloat(SG1.Cells[i,j]); end; for i:= 0 to b-1 do for j:= 0 to c-1 do begin MatrixB[i,j]:=StrToFloat(SG2.Cells[i,j]); end; for i := 0 to a-1 do for j := 0 to c-1 do for k := 0 to b-1 do begin matrixc[i,j] := matrixc[i,j] + (matrixa[i,k] * matrixb[k,j]); SG3.Cells[i,j]:=FloatToStr(matrixc[i,j]); end; end; |
Re: Matrizen-Multiplikation
Wo tritt der Fehler genau auf?
|
Re: Matrizen-Multiplikation
Die Meldung legt doch nahe, dass dein Fehler dann auftritt, wenn du versuchst einen leeren String in ein Float umzuwandeln. Der Fehler dürfte also im Einlesen der grids in die Arrays liegen. Sind denn alle Cells der Grids mit einer Zahl (z.B. 0) gefüllt?
|
Re: Matrizen-Multiplikation
Ausserdem hätte wohl ein Thread auch gereicht, um ein und das gleiche Problem zu erklären:
![]() Fehlermeldungen darf man ruhig mal lesen und versuchen zu verstehen. Zitat:
|
Re: Matrizen-Multiplikation
Der Fehler befindet sich nach meinen Kenntinisstand beim einlesen des zweiten Stringgrids,
also in etwa hier.
Delphi-Quellcode:
Sorry das ich euer Forum überflute, aber ich weiß an der Stelle wirklich nicht weiter, da
for i:= 0 to b-1 do
for j:= 0 to c-1 do begin MatrixB[i,j]:=StrToFloat(SG2.Cells[i,j]); end; ich nicht weiß, warum hier eine Zelle keine Gleitkommazahl enthalten sollte. Es sind definitiv alle existierenden Stringgridcells mit Zahlen belegt (mach ich vorher mit random). Hab ich vielleicht ein Array falsch mit Indizes versehen ? |
Re: Matrizen-Multiplikation
Hallo,
vielleicht kommst du deinem Fehler so auf die Spur:
Delphi-Quellcode:
Grüße vom marabu
// ...
for i:= 0 to b-1 do for j:= 0 to c-1 do if (i < SG2.ColCount) and (h < SG2.RowCount) then MatrixB[i,j] := StrToFloat(SG2.Cells[i,j]) else ShowMessage(Format('Index out of Bounds: SG2.Cells[%d,%d]', [i, j])); |
Re: Matrizen-Multiplikation
Danke für den Vorschlag, habs aber jetzt doch selbst hingekriegt (mehr oder weniger).
Hier erstmal meine Änderungen.
Delphi-Quellcode:
In den meisten Fällen funzt das jetzt. Will ich nun jedoch z.B. eine 4x2
procedure TForm3.Matrixmul(Sender:TObject);
var matrixA: array of array of double; matrixB: array of array of double; matrixC: array of array of double; a,b,c,i,j,k:integer; begin a:=StrToInt(EditA.Text); b:=StrToInt(EditB.Text); c:=StrToInt(Editb.Text); SetLength(matrixA,a,b); SetLength(matrixB,b,c); SetLength(matrixc,a,c); for i := 0 to a-1 do for j := 0 to c-1 do begin matrixc[i,j] := 0; SG3.Cells[j,i]:='0'; end; For i:= 0 to SG1.RowCount-1 do for j:= 0 to SG1.ColCount-1 do begin matrixA[i,j]:=StrToFloat(SG1.Cells[j,i]); end; for i:= 0 to SG2.RowCount-1 do for j:= 0 to SG2.ColCount-1 do begin MatrixB[i,j]:=StrToFloat(SG2.Cells[j,i]); end; for i := 0 to SG1.RowCount-1 do for j := 0 to SG3.ColCount-1 do for k := 0 to SG2.RowCount-1 do begin matrixc[i,j] := matrixc[i,j] + (matrixa[i,k] * matrixb[k,j]); SG3.Cells[j,i]:=FloatToStr(matrixc[i,j]); end; end; mit einer 2x4 Matrix multiplizieren, so erhalte ich eine neue Fehlermeldung. "ungültige Zeigeroperation" Was soll das denn bedeuten? Danke für eure Hilfe |
Re: Matrizen-Multiplikation
Du brauchst zwei Konvertierungsfunktionen:
StringGrid -> Matrize Matrize -> Stringgrid Hier die Deklarationen:
Delphi-Quellcode:
Wichtig: die Anzahl von Reihen und Spalten der Matrix muss zum StringGrid passen.
type
T2DMatrix = array of array of double; ... procedure StringgridToMatrix(const sg:TStringGrid; var matrix:T2DMatrix); procedure MatrixToStringgrid(const matrix:T2DMatrix; sg:TStringGrid); Das lässt sich aber per Code überprüfen. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:47 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz