使用代表向量的矩阵乘法算法

时间:2014-05-19 10:24:50

标签: algorithm matlab matrix

作为课程练习,我必须将下三角矩阵转换为矢量,例如[a 0; b d] - > [a b d]。之后,我必须在MATLAB中编写一个只使用矢量模式进行矩阵乘法的算法。

任何人都可以帮我算法吗?

1 个答案:

答案 0 :(得分:0)

这里我的代码在零停留在向量中时有效。如果零没有留在你的载体中,不知道是否有办法解决你的问题。

mytest_Mat1 = round(rand(3,4)*10);
mytest_Mat2 = round(rand(4,5)*10);
mysize_Mat1 = size(mytest_Mat1);
mysize_Mat2 = size(mytest_Mat2);
if mysize_Mat1(2)==mysize_Mat2(1)
mytest_vec1= reshape(mytest_Mat1,1, mysize_Mat1(1)*mysize_Mat1(2));
mytest_vec2= reshape(mytest_Mat2,1, mysize_Mat2(1)*mysize_Mat2(2));
mytest_result = zeros(1,mysize_Mat1(1)*mysize_Mat2(2));
for m=1:mysize_Mat1(1)
   for n=1:mysize_Mat2(2)
        mytest_helper =0;
        for o=1:mysize_Mat1(2)
            mytest_helper = (mytest_helper+mytest_vec1(m+(o-1)*mysize_Mat1(1))*mytest_vec2((n-1)*mysize_Mat2(1)+o));            
        end
        mytest_result((n-1)*mysize_Mat1(1)+m)= mytest_helper;
   end
end
mytest_MatMult = mytest_Mat1*mytest_Mat2
mytest_result
else mytest_error = ('The dimensions of the matrix do not fit!')
end

mytest_helper计算最终保存在mytest_result中的元素。