Didon
Didon

Reputation: 393

Conjugate of a complex number in Matlab

In order to convert a Matlab code to C, I want to write it in a similar way to C first then its translation would become trivial. I faced a problem with this line:

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

Where E is (9x4) real matrix and SOLS is (3x10) complex matrix. A should be a 9x10 complex matrix.

I translated this line as follows:

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

I got the same result. When I replaced conj(X) by real(X)-i*imag(X)for example:

conj(SOLS(j,1))  by `real(SOLS(j,1))-imag(SOLS(j,1))*i`, 

I got a wrong result and I don't understand why.

I'm doing this because in the C code, every complex number is represented by a struct with variable.re the real part and variable.im the imaginary part.

typedef struct COMPLEX{
    float re;
    float im;
}Complex;

I want to write a very similar matlab code to C to manipulate variables easily in C with getting a similar result with Matlab.

How to correct this please?

Upvotes: 0

Views: 353

Answers (1)

Buck Thorn
Buck Thorn

Reputation: 5073

You are using i both as a looping index and sqrt(-1). If you want to replace conj(SOLS(j,1)) use instead

 real(SOLS(j,1))-imag(SOLS(j,1))*1i 

Upvotes: 2

Related Questions