% Flops Comparison for Matrix Multiplication Demonstration
%
% Prepared by: Douglas Meade
%              26 August 2005

m = 3             % dimensions of matrices
n = 10
p = 2
k = 18

A = rand(m,n);    % random mxn matrix
B = rand(n,p);    % random nxp matrix
C = rand(p,k);    % random pxk matrix

flops(0)          % reset flops counter to zero
P1 = (A*B)*C;
f1 = flops        % number of flops to compute P1

flops(0)          % reset flops counter to zero
P2 = A*(B*C);
f2 = flops        % number of flops to compute P2

% what can you discover about the way MATLAB computes products?

flops(0)         % reset flops counter to zero
P0 = A*B*C;
f0 = flops       % number of flops to compute P0

P1-P0
P2-P0