Can someone explain this?

조회 수: 6 (최근 30일)
Alex Bernadí Forteza
Alex Bernadí Forteza 2022년 7월 31일
답변: Alex Bernadí Forteza 2022년 7월 31일
Hi everyone,
Something strange is happening in my code and I would like ti figure out. I have a nested loop like this:
sum_F = zeros(2048,188);
% E is a 1x2048 complex vector
for j = 1:188
disp(j)
v = zeros(1,2048);
for i = 1:2048
if find(freqind==i) % condition to reduce the number of points to be calculated
T = mat_T(nodes_steps(:,:,j), points, ka(i)); % nodes_steps is a 2080x3x188 tensor, points is a 3x3 matrix and k is a complex number
R = SL2_r(i).*eye(n_r); % diagonal matrix
R = sparse(R);
F = T.'*(R*sum(T,2)); % F is a 2080x1 complex vector
v(i) = sum(F)*E(i); %<---- case 1.
sum_F(i,j) = sum(F)*E(i); %<---- case 2
end
end
sum_FR(:,j) = v'.*exp(1i*2*pi*(frecs')/ct*z0); % <---- case 1
sum_FR(:,j) = sum_F(:,j).*exp(1i*2*pi*(frecs')/ct*z0); % <----- case 2
end
At first view everyone should think, as I did, that in both cases the value assignated to sum_FR(:,j) in every j-loop should be the same regardless of the case because in case 1 I am multiplying a column vector by a column vector. And in case 2 I am multiplying the same column vector by the other same column vector, and also v and the columns of matrix sum_F are calculated the same way. But in reality the results obtained in the case 1 are simetrics with obtained in case 2. As shown in the pictures:
Case 1
Case 2
When I realized this I started to investigate to see what was going on. And I found out that the norm between v' and sum_FR(:,j) is not 0 but 0.0333 and to me it doesn't make any sense because they should be the same vector. If I declare v as a column vector the norm between both vectors is now 0 as it should be. Can someone explain me what is happening? Why when I transpose the vector their values vary? Or why it has a different value if I declare it as a row vector or as a column vector?
P.S.: The correct result is the case 2
Thank you all!!
  댓글 수: 2
Alex Bernadí Forteza
Alex Bernadí Forteza 2022년 7월 31일
What is happening here is that v is a complex vector so if I perfom v' it results the transposed conjugate vector and that why norm( v' - sum_FR(j:enf)) ~=0. To fix that you just need to calculate the transposed vector using .' instead of '
Jan
Jan 2022년 7월 31일
Correct. Please post this as an answer, such that it can be selected as a solution.

댓글을 달려면 로그인하십시오.

채택된 답변

Alex Bernadí Forteza
Alex Bernadí Forteza 2022년 7월 31일
What is happening here is that v is a complex vector so if I perfom v' it results the transposed conjugate vector and that why norm( v' - sum_FR(j:enf)) ~=0. To fix that you just need to calculate the transposed vector using .' instead of '

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by