gpt4 book ai didi

c - 在 C 中使用转置的矩阵乘法

转载 作者:行者123 更新时间:2023-12-02 08:45:29 25 4
gpt4 key购买 nike

我是 C 的初学者。我试图编写一些代码来使用转置执行矩阵乘法。有什么方法可以在执行时间方面改进代码?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <time.h>

int main()
{

int a[3][3] = {{1,0, 1}, {2, 2, 4},{1, 2, 3}};

int b[3][3] ={ { 2, 3, 1}, { 6, 6, 2 }, { 9, 9, 0 } };
int result[3][3];
double tmp;
int i,j,k;
for (i=0; i<3; i++) //i = col
{
for (k=0; k<3; k++)
{
tmp = a[i][k];
for (j=0; j<3; j++) //j = row
{
result[i][j] += tmp * b[k][j];
printf("%d\t",result[i][j]);
}
}
}
}

最佳答案

如果您的矩阵是int,您真的不应该使用double 作为临时矩阵。无目的地将整数转换为 float 并再次返回是非常浪费的,它可能会花费很多。

关于c - 在 C 中使用转置的矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12795143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com