Mathematics 726

Example for matrix product as linear combination of rows

By Li Chong


% FILE: rowmat.m   (Ma726)
% PROVIDES: MatLab function rowmat(A, B) to compute 
%           product of matrix A and matrix B by using
%           linear combination of rows of B.
%
% Homework Ma726 
% Author: Chong Li 
% Date:   Jan. 23, 1998
%
% Input:  A = m by p matrix
%         B = p by n matrix
% Output: Matrix product AB
%
% Call syntax:  C = rowmat(A,B) or rowmat(A,B)
% Precondition: Number of columns of A is equal to
%               number of rows of B.

 function C = rowmat(A,B);

 [m,p] = size(A); 
 [q,n] = size(B);  

 if p ~= q
    disp('Error: Matrix sizes do not match. ');
 else
    C = zeros(m,n);
    for i=1:m
        for k=1:p
            C(i,:) = C(i,:) + A(i,k)*B(k,:); % Linear combination of rows of B. 
        end
    end
 end

% NOTE: if using ans = C, then get ans twice.
% EOF

Home