# How to multiply matrices using for loops?

610 views (last 30 days)
Stefan on 14 Sep 2014
Answered: Kan Sun on 22 Jan 2019
I have a problem in which I have to multiply two matrices, x (700x900) and y(900,1100), using a for loop. I'm not sure where to start, I've only been using MATLAB for about a month. Any advice/help/suggestions would be great!
Thank You

Image Analyst on 14 Sep 2014
Try this:
x = randi(9, 700, 900); % Sample data.
y = randi(9, 900, 1100);
[rowsx, colsx] = size(x);
[rowsy, colsy] = size(y);
theMatrixProduct = zeros(rowsx, colsy);
for row = 1 : rowsx
row % Print progress to command window.
for col = 1 : colsy
theSum = 0;
for k = 1 : colsx
theSum = theSum + x(row, k) * y(k, col);
end
theMatrixProduct(row, col) = theSum;
end
end
Image Analyst on 17 Dec 2016
It's NOT wrong. I don't know why people keep doubting it. Look, here is the full code (changed to be smaller matrices than the 700x900) with the arrays being printed to the command window so that you can see they match up perfectly:
x = randi(9, 7, 9); % Sample data.
y = randi(9, 9, 11);
[rowsx, colsx] = size(x);
[rowsy, colsy] = size(y);
theMatrixProduct = zeros(rowsx, colsy);
for row = 1 : rowsx
row % Print progress to command window.
for col = 1 : colsy
theSum = 0;
for k = 1 : colsx
theSum = theSum + x(row, k) * y(k, col);
end
theMatrixProduct(row, col) = theSum;
end
end
% Do it the usual way, with matrix multiplication instead of for loops.
theMatrixProduct2 = x * y;
% Print both to the command window
theMatrixProduct2
theMatrixProduct
% Subtract to find the differences.
theDiff = theMatrixProduct2 - theMatrixProduct;
% If they're equal the max difference will be 0.
fprintf('Max difference = %f\n', max(theDiff(:)));
% It is zero, so the match.
In the command window you'll see the proof:
theMatrixProduct2 =
176 199 260 207 157 123 263 200 174 282 257
161 155 166 124 115 110 165 143 154 204 209
208 232 213 201 157 119 210 197 173 257 256
181 243 267 206 144 138 262 199 241 296 295
191 200 210 180 195 141 264 221 171 287 246
297 305 315 305 203 155 315 290 250 333 358
276 257 266 256 192 143 283 282 237 303 327
theMatrixProduct =
176 199 260 207 157 123 263 200 174 282 257
161 155 166 124 115 110 165 143 154 204 209
208 232 213 201 157 119 210 197 173 257 256
181 243 267 206 144 138 262 199 241 296 295
191 200 210 180 195 141 264 221 171 287 246
297 305 315 305 203 155 315 290 250 333 358
276 257 266 256 192 143 283 282 237 303 327
Max difference = 0.000000
See, they're both the same and the differences are all zero.
Now it's your turn to show why the code is not correct by posting an example of where the matrices calculated the two different ways are different. Please provide your counter example so I can debug and correct my code (if needed).

Kan Sun on 22 Jan 2019
Suppose you have matrix1(N*M) and matrix2(M*L), then you can have the product using for loops as following:
product=zeros(N,L);
for i=1:N
for j=1:L
product(i,j)=matrix1(i,1)*matrix2(1,j);
for k=2:M
product(i,j)=product(i,j)+matrix1(i,k)*matrix2(k,j);
end
end
end
product1=product
At last you can compare this product1 to the result of matrix1*matrix2 to see if they are the same (yes they are).