如何在MatLab中编程一个函数,将两个矩阵A*B作为输入,然后输出乘积矩阵A*B?[复制]

idv4meu8  于 2022-11-15  发布在  Matlab
关注(0)|答案(1)|浏览(216)

这个问题这里已经有答案了

How do I program a function that takes two matrices A and B as input and outputs the product matrix A*B?(1个应答)
上个月已关闭。此问题来自超级用户migrated,因为它可以在堆栈溢出时得到回答。上个月Migrated

问题:如何编写一个以两个矩阵A和B为输入并输出乘积矩阵A*B的函数?使用MatLab,与“for”或“While”类似的东西,即循环或条件句。
尝试

function prodAB=MultiplicoMatrices(A,B)

prod=0;

prodAB=[];

for i=1:length(A)

    for j=1:length(B)

        prod=prod+A(i,j)*B(j,i);

    end

    prodAB(i,j)=prod;

    prod=0;

end

A =

     1     2
     3     4

 B=[5 6 ; 7 8]

B =

     5     6
     7     8
>> prodAB=MultiplicoMatrices([1 2; 3 4],[5 6; 7 8])

prodAB =

     0    19
     0    50
watbbzwu

watbbzwu1#

function prodAB = MultiplicoMatrices(A,B)
% Initiate output matrix. It will have the same number of rows as A and 
% same number of columns as B
prodAB = zeros(size(A,1),size(B,2));

% Calculate value for every element in output matrix
for row = 1:size(prodAB,1)
    for col = 1:size(prodAB,2)
        
        % Increment using the rows in b or columns in a (they have to be
        % the same anyway)
        for inc = 1:size(B,1)
            prodAB(row,col) = prodAB(row,col) + (A(row,inc)*B(inc,col));     
        end
    end
end
end

相关问题