필터 지우기
필터 지우기

Why do unexpected imaginary values appear when I try to fill my matrix with a loop?

조회 수: 6 (최근 30일)
Evelyn
Evelyn 2014년 5월 4일
편집: Matt J 2014년 5월 5일
The entire code is too big to put here, but I have
Ntot = Nq*Nw;
matrix = zeros(Ntot,4);
Nline = 0;
for iq=1:Nq
for iw=1:Nw
Nline = Nline+1;
matrix(Nline,1) = q(iq);
matrix(Nline,2) = iw;
matrix(Nline,3) = pq(iq)/Nw;
matrix(Nline,4) = function(q(iq), w(iw,:), u(iw,:), r(iw,:));
end
end
with q and pq vectors with only real numbers. The function in line 4 is also working. The first loop (of iq, so for several lines) goes as expected, but in the second loop suddenly unwanted and unexpected imaginary numbers appear in ALL the columns. Can anyone help me with this?
  댓글 수: 6
Evelyn
Evelyn 2014년 5월 5일
편집: Evelyn 2014년 5월 5일
I changed the name of the function to a more general name, the real name is 'max_meerpeil' and it uses a value of q and three rows, from w, u and r, to calculate one value. I tried it with a random value and random rows, and it works as it should. For the first loop of iq (so for several lines) it also works as it should, just as the other lines. In the second loop of iq it starts to act weird.
Evelyn
Evelyn 2014년 5월 5일
편집: Evelyn 2014년 5월 5일
Walter (or someone else), do you have a suggestion how to get the rows out of w, r and u otherwise?

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

답변 (1개)

Matt J
Matt J 2014년 5월 5일
편집: Matt J 2014년 5월 5일
Trap the line that generates complex values by Setting A Conditional Breakpoint. Logical places to do this would be all the lines in the loop that assign to "matrix". The relevant condition would be
nnz(imag(matrix))>0
Do similarly inside whatever "function" is now called until you find the exact line.

카테고리

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