Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Special matrix multiplication
Replies: 18   Last Post: Mar 22, 2012 5:12 AM

 Messages: [ Previous | Next ]
 Milos Milenkovic Posts: 189 Registered: 4/4/09
Re: Special matrix multiplication
Posted: Mar 19, 2012 11:25 AM

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.

function [C]=prodrs(A,B)
[a,b]=size(A);
[c,d]=size(B);
a = size(A,1);%broj vrsta matrice
A1 = A(:,1:3:end); A2 = A(:,2:3:end); A3 = A(:,3:3:end);
B1 = B(:,1:3:end); B2 = B(:,2:3:end); B3 = B(:,3:3:end);
C1 = zeros(a,3*c); C3 = zeros(c,3*d);
for i1 = 1:a
for i2 = 1:a
if A2(i1,i2) > 0
for i3 = 1:3*c
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)*A3(i1,i2)+A2(i1,i2)*(B1(i2,i3)-B2(i2,i3));
C3(i1,i3) = C3(i1,i3)+B2(i2,i3)*A1(i1,i2)+A2(i1,i2)*(B3(i2,i3)-B2(i2,i3));
end
end
else
for i3 = 1:3*c
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;
C = zeros(a,d);
C(:,1:3:end) = C1; C(:,2:3:end) = C2; C(:,3:3:end) = C3;

Best,
Milos

"Roger Stafford" wrote in message <jk044p\$sm0\$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jju47n\$74f\$1@newscl01ah.mathworks.com>...
> > "Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <jjtkgm\$k9n\$1@newscl01ah.mathworks.com>...
> > > Dear Roger,
> > > thanks again, I already tried to modify your first code on just the same way as you proposed, but the result is not correct, I will try tomorrow and then ask for help. Do you have some good propossal for if statement, how to check the positiveness of bi and ei?
> > > This is the procedure for fuzzy numbers multiplying, so I used it in multiplying matrices composed from fuzzy numbers.
> > > ........

> > - - - - - - - - - -
> > A1 = A(:,1:3:n); A2 = A(:,2:3:n); A3 = A(:,3:3:n);
> > B1 = B(:,1:3:n); B2 = B(:,2:3:n); B3 = B(:,3:3:n);
> > A2P = (A2>0); B2P = (B2>0);
> > S1 = (A2P&B2P); S2 = (A2P~=B2P); S3 = ~(A2P|B2P);
> > C1 = S1.*(A2*B1+(A1-A2)*B2)+S2.*(A1*B2+A2*(B3-B2))+S3.*(A3*B2+A2*(B3-B2));
> > C2 = A2*B2;
> > C3 = S1.*(A3*B2+A2*(B3-B2))+S2.*(A3*B2+A2*(B1-B2))+S3.*(A1*B2+A2*(B1-B2));
> > C(:,3:3:end) = C3; C(:,2:3:end) = C2; C(:,1:3:end) = C1;

> - - - - - - - -
> You should ignore that last solution I sent you, Milos. It occurred to me late last night that it can't possibly work. I believe it is inherent in "fuzzy" computation, as you have modified it to be dependent on the signs of those middle terms, that only the middle term can be carried out by the regular matrix multiplication operation. The other two terms need to be done using for-loops, nested three deep. There seems no getting around that.
>
> The following is such a routine, but you cannot expect it to be anywhere near as fast as regular matrix multiplication. It is assumed here that A and B are of size n by 3*n. If you wish to generalize it to an m-by-3*n times n-by-3*p size multiplication, it should be easy to modify it accordingly.
>
> n = size(A,1);
> A1 = A(:,1:3:3*n); A2 = A(:,2:3:3*n); A3 = A(:,3:3:3*n);
> B1 = B(:,1:3:3*n); B2 = B(:,2:3:3*n); B3 = B(:,3:3:3*n);
> C1 = zeros(n); C3 = zeros(n);
> for i1 = 1:n
> for i2 = 1:n
> if A2(i1,i2) > 0
> for i3 = 1:n
> 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:n
> 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;
> C = zeros(n,3*n);
> C(:,1:3:3*n) = C1; C(:,2:3:3*n) = C2; C(:,3:3:3*n) = C3;
>
> The result is in C.
>
> Roger Stafford