|
|
Re: Special matrix multiplication
Posted:
Mar 22, 2012 5:12 AM
|
|
Dear Roger, the code works excellent. Thanks!!!!! Best, Milos "Roger Stafford" wrote in message <jk7u73$icn$1@newscl01ah.mathworks.com>... > "Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <jk7j4n$7as$1@newscl01ah.mathworks.com>... > > Dear Roger, > > this is great, thanks!!!! Just please little help with transforming the code in universal form. Dimensions are frequently the form you said, (4x3*4)*(4*3*14), I tried but somewhere I mistake. > > ........ > - - - - - - - - - - > If your temporary matrices are of the sizes: > > A1, A2, A3 --> p by q > B1, B2, B3 --> q by r > C1, C2, C3 --> p by r > > then the following code remains almost the same with only the appropriate replacement of n by p, q, and r in the for-loop counters: > > C1 = zeros(p,r); C3 = zeros(p,r); > for i1 = 1:p > for i2 = 1:q > if A2(i1,i2) > 0 > for i3 = 1:r > if B2(i2,i3) > 0 > C1(i1,i3) = C1(i1,i3)+A2(i1,i2)*B1(i2,i3)+B2(i2,i3)*(A1(i1,i2)-A2(i1,i2)); > C3(i1,i3) = C3(i1,i3)+B2(i2,i3)*A3(i1,i2)+A2(i1,i2)*(B3(i2,i3)-B2(i2,i3)); > else > C1(i1,i3) = C1(i1,i3)+B2(i2,i3)*A1(i1,i2)+A2(i1,i2)*(B3(i2,i3)-B2(i2,i3)); > C3(i1,i3) = C3(i1,i3)+B2(i2,i3)*A3(i1,i2)+A2(i1,i2)*(B1(i2,i3)-B2(i2,i3)); > end > end > else > for i3 = 1:r > if B2(i2,i3) > 0 > C1(i1,i3) = C1(i1,i3)+B2(i2,i3)*A1(i1,i2)+A2(i1,i2)*(B3(i2,i3)-B2(i2,i3)); > C3(i1,i3) = C3(i1,i3)+B2(i2,i3)*A3(i1,i2)+A2(i1,i2)*(B1(i2,i3)-B2(i2,i3)); > else > C1(i1,i3) = C1(i1,i3)+B2(i2,i3)*A3(i1,i2)+A2(i1,i2)*(B3(i2,i3)-B2(i2,i3)); > C3(i1,i3) = C3(i1,i3)+B2(i2,i3)*A1(i1,i2)+A2(i1,i2)*(B1(i2,i3)-B2(i2,i3)); > end > end > end > end > end > C2 = A2*B2; > > How you extract A1, A2, A3, B1, B2, B3 from A and B, and how you insert C1, C2, and C3 back into C depends on the particular format you wish to use in these matrices. Define carefully the format and I'll show you the extraction and insertion code if I can. I can think of a number of different formats you might conceivably use, one of them using three-dimensional arrays. > > Roger Stafford
|
|