Explicit multiplication of matrices using for loops
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
dpb
il 13 Nov 2013
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
il 13 Nov 2013
And FYI, you don't need a semicolon at the end of a "for" line of code.
Risposte (1)
David Sanchez
il 13 Nov 2013
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 Commenti
Vedere anche
Categorie
Scopri di più su Construct and Work with Object Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!