Explicit multiplication of matrices using for loops
이전 댓글 표시
Hi, I'm just getting to grips with for loops in Matlab, and I encountered a problem in understanding this specific code:
x = [4; 0.5 ; 1];
A = [6 8 1 ; 3 10 1 ; 1 0.5 12.5];
B = [2 0.5 1 ; 0 0.75 0.5 ; 0.5 4 1.5];
for ix = 1:3;
yex(ix)=0;
for jx = 1:3;
yex(ix) = yex(ix) + A(ix,jx) * x(jx);
end
end
for ix = 1:3;
for jx = 1:3;
Cex(ix,jx) = 0;
for kx = 1:3 ;
Cex(ix,jx) = Cex(ix,jx) + A(ix,kx) * B(kx,jx);
end
end
end
As you can see, it's a way of explicitly multiplying matrices using for loops. My problem is understanding the explicit part, and what each line of code is doing. Help in understanding this would be much appreciated. Thanks.
댓글 수: 2
dpb
2013년 11월 13일
What, specifically don't you understand?
BTW, it would be more efficient to move the initializations outside the loops --
yex=zeros(1,3);
for ix = 1:3;
...
and
Cex=zeros(3);
for ix = 1:3;
for jx = 1:3;
...
respectively.
Not sure what your question is really asking -- they compute two different sums, the latter one of which is the formal definition of a matrix multiplication. What do you consider 'explicit' as opposed to the rest, maybe?
Image Analyst
2013년 11월 13일
And FYI, you don't need a semicolon at the end of a "for" line of code.
답변 (1개)
David Sanchez
2013년 11월 13일
for ix = 1:3; % loop along the elements of yex
yex(ix)=0; % initialization of ix-th element of yex to zero
for jx = 1:3; % iterate along the column elements of A in the ix-th row
yex(ix) = yex(ix) + A(ix,jx) * x(jx); % update the value of yex, the yex previous value is added to the jx-th column element of A plus the jx-th value of x
end
end
The other for-loop is the same but with an extra dimension.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!