首页 > 后端开发 > C++ > 正文

矩阵乘法和归一化在C程序中的实现

WBOY
发布: 2023-09-07 09:37:16
转载
773人浏览过

矩阵乘法和归一化在c程序中的实现

矩阵乘法

现在讨论矩阵乘法的过程。矩阵乘法只有满足一定的条件才能进行。假设两个矩阵是 P 和 Q,它们的维度是 P (a x b) 和 Q (z x y),当且仅当 b = x 时才能找到结果矩阵。那么所得矩阵 R 的阶将为 (m x q)。

算法

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
登录后复制

矩阵标准化

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))
登录后复制

示例

#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>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/1586">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680266786298.png" alt="乾坤圈新媒体矩阵管家">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/1586">乾坤圈新媒体矩阵管家</a>
                            <p>新媒体账号、门店矩阵智能管理系统</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="乾坤圈新媒体矩阵管家">
                                <span>17</span>
                            </div>
                        </div>
                        <a href="/ai/1586" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="乾坤圈新媒体矩阵管家">
                        </a>
                    </div>
                <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;
}
登录后复制

输出

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.
登录后复制

以上就是矩阵乘法和归一化在C程序中的实现的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号