Heim  >  Artikel  >  Backend-Entwicklung  >  Implementierung der Matrixmultiplikation und -normalisierung in einem C-Programm

Implementierung der Matrixmultiplikation und -normalisierung in einem C-Programm

WBOY
WBOYnach vorne
2023-09-07 09:37:16579Durchsuche

Implementierung der Matrixmultiplikation und -normalisierung in einem C-Programm

Matrixmultiplikation

Besprechen Sie nun den Prozess der Matrixmultiplikation. Eine Matrixmultiplikation kann nur durchgeführt werden, wenn bestimmte Bedingungen erfüllt sind. Angenommen, die beiden Matrizen sind P und Q und ihre Dimensionen sind P (a x b) und Q (z x y). Die resultierende Matrix wird genau dann gefunden, wenn b = x. Dann ist die Ordnung der resultierenden Matrix R (m x q).

Algorithmus

matrixMultiply(P, Q):
Assume dimension of P is (a x b), dimension of Q is (z x y)
Begin
   if b is not same as z, then exit
   otherwise define R matrix as (a x y)
   for i in range 0 to a - 1, do
      for j in range 0 to y – 1, do
         for k in range 0 to z, do
            R[i, j] = R[i, j] + (P[i, k] * Q[k, j])
         done
      done
   done
End

Matrixnormalisierung

Suppose we have a 2x3 matrix:
4 5 6
1 2 3 The normalized matrix would be:
4/sqrt(pow(5,2) + pow(6,2)) 5/sqrt(pow(5,2) + pow(6,2)) 6/sqrt(pow(5,2) + pow(6,2))
1/sqrt(pow(2,2) + pow(3,2)) 2/sqrt(pow(2,2) + pow(3,2)) 3/sqrt(pow(2,2) + pow(3,2))

Beispiel

#include <stdio.h>
#include <math.h>
int main() {
   int row, col, row1, col1;
   int assignMatrix[50][50], rowAdd[100] = {0};
   long long int a, square[50] = {0};
   double norm[50][50], k;
   printf("Enter size of a matrix</p><p>");
   scanf("%d %d", &row, &col);
   printf("Enter matrix of size %dX%d</p><p>", row, col);
   for ( row1 = 0; row1 < row; row1++) {
      for (col1 = 0; col1 < col; col1++) {
         scanf("%d", &assignMatrix[row1][col1]);
      }
   }
   printf("</p><p>rows: %d cols: %d elements:</p><p>",row,col);
   for( row1 = 0; row1 < row; row1++) {
      for( col1 = 0; col1 < col; col1++) {
         printf("%d ", assignMatrix[row1][col1]);
      }
      printf("</p><p>");
   }
   for (row1 = 0; row1 < row; row1++) {
      for (col1 = 1; col1 < col; col1++) {
         a = assignMatrix[r][c];
         square[row1] += a * a;
      }
      printf("Sum of squares of row %d: %lld</p><p>",row1,square[row1]);
   }
   for ( row1 = 0; row1 < row; row1++ ) {
      k = 1.0 / sqrt(square[row1]);
      for( col1 = 0; col1 < col; col1++ ) {
         norm[row1][col1] = k * assignMatrix[row1][col1] ;
      }
   }
   printf("</p><p>Normalized Matrix:</p><p>");
   for( row1 = 0; row1 < row; row1++) {
      for( col1 = 0; col1 < col; col1++) {
         printf("%.3lf ", norm[row1][col1]);
      }
      printf("</p><p>");
   }
   return 0;
}

Ausgabe

Enter size of a matrix
2
3
Enter matrix of size 2X3
4 5 6
1 2 3
rows: 2 cols: 3 elements:
4 5 6
1 2 3
Sum of squares of row 0: 61
Sum of squares of row 1: 13
Normalized Matrix:
0.512 0.640 0.768
0.277 0.555 0.832
Process returned 0 (0x0) execution time : 12.446 s
Press any key to continue.

Das obige ist der detaillierte Inhalt vonImplementierung der Matrixmultiplikation und -normalisierung in einem C-Programm. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen