Why do unexpected imaginary values appear when I try to fill my matrix with a loop?
Show older comments
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 Comments
Evelyn
on 4 May 2014
Azzi Abdelmalek
on 4 May 2014
What is function?
Star Strider
on 4 May 2014
More specifically, we need to know what this line:
matrix(Nline,4) = function(q(iq), w(iw,:), u(iw,:), r(iw,:));
is doing. What is ‘function’ and what is it returning?
(One caution: function is a reserved word in MATLAB. Please do not use that name for your own functions or variables.)
Walter Roberson
on 5 May 2014
Speculation: the function on the 4th line is not okay, and is using assignin('caller')
Answers (1)
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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!