필터 지우기
필터 지우기

How to multiply matrices using for loops?

조회 수: 65 (최근 30일)
Stefan
Stefan 2014년 9월 14일
댓글: Aditya Sur 2022년 3월 6일
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
Image Analyst 2014년 9월 14일
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
  댓글 수: 5
Sunil Kunjachan
Sunil Kunjachan 2016년 12월 17일
this wrong. please show resultant array
Image Analyst
Image Analyst 2016년 12월 17일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kan Sun
Kan Sun 2019년 1월 22일
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).
  댓글 수: 1
Aditya Sur
Aditya Sur 2022년 3월 6일
I did'nt understand this statement: product(i,j)=matrix1(i,1)*matrix2(1,j);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by