Explicit multiplication of matrices using for loops
2 次查看(过去 30 天)
显示 更早的评论
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?
回答(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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!