use for loops that will matrix multiply two 3 by 3 matrices in matlab in matlab

main.m
% define two 3x3 matrices
A = [1 2 3; 4 5 6; 7 8 9];
B = [9 8 7; 6 5 4; 3 2 1];

% create matrix C to store results
C = zeros(3);

% iterate through rows of A
for i=1:3 
    % iterate through columns of B
    for j=1:3
        % iterate through rows of B and sum products
        for k=1:3
            C(i,j) = C(i,j) + A(i,k)*B(k,j);
        end
    end
end

% print result
disp(C);
384 chars
21 lines

This program defines the two matrices A and B, creates an empty matrix C to store the products, and then iterates through the rows and columns of A and B using for loops. It multiplies the elements of each row in A by the corresponding elements in each column of B to get the corresponding element in C using another for loop. Finally, it prints out the resulting matrix C.

gistlibby LogSnag