Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

matrix - Multiplying complex matrices in Matlab

I'm trying to port a code from matlab to C. In order to convert this line to C:

A = E*[SOLS' ; ones(1,10 ) ]; 

Where,

>>size(SOLS)

ans =

10     3

and:

>> size(E)

ans =

 9     4

SOLS is a complex single matrix and E is a real single matrix and A should be a complex single matrix of size 9x10.

I replaced A = E*[SOLS' ; ones(1,10 ) ]; with

for i=1:9
  for j=1:10
    A1(i,j)=E(i,1)*SOLS(j,1))+E(i,2)*SOLS(j,2))+E(i,3)*SOLS(j,3))+E(i,4);
   end
end

The complex resultant matrix elements have the same real part as A but a different imaginary part.

>> real(A)=real(A1)
imag(A) and `imag(A1)` are different.

What caused this difference? How to convert the matlab command correctly to C?

Here are examples of matrices:

    E =

    0.2248         0         0         0
   -0.4487   -0.1632   -0.1955    0.6355
    0.4379   -0.0651   -0.1032   -0.0754
   -0.4008    0.3513    0.2707   -0.5936
   -0.2294   -0.7853   -0.3290   -0.4648
    0.0385    0.2623   -0.6363   -0.0978
   -0.5716    0.0851    0.0943    0.0587
    0.1160   -0.3911    0.5964    0.0947
    0.0363   -0.0039   -0.0092   -0.0018

and

    SOLS =

   1.0e+02 *

  -0.2410 + 0.0000i   2.3741 + 0.0000i  -0.0646 + 0.0000i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0113 - 0.0046i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0113 + 0.0046i
  -0.0028 + 0.0000i  -0.0114 + 0.0000i  -0.0038 + 0.0000i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0024 - 0.0043i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0024 + 0.0043i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0007 - 0.0191i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0007 + 0.0191i
  -0.0080 + 0.0000i   0.0064 + 0.0000i   0.0108 + 0.0000i
  -0.7289 + 0.0000i   4.9347 + 0.0000i   0.3841 + 0.0000i
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In MATLAB, SOLS' performs the complex conjugate transpose operation, that is element {i,j} becomes element {j,i} and its value is transformed as a + 1i*b --> a -1i*b. To retain the phase of your complex values use SOLS.' as follows:

A = E*[SOLS.' ; ones(1,10 ) ];

In addition this is how you want to perform the loop (translating of course to proper C):

for i=1:size(E,1)
  for j=1:size(SOLS,1)
        A1(i,j)=0;
        for k = 1:size(SOLS,2)
            A1(i,j)= A1(i,j) + E(i,k)*SOLS(j,k);
        end
        A1(i,j)= A1(i,j) + E(i,k+1);
   end
end

Then

A1 - A

ans =

   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...