I have an annoying situation I cannot resolve with my google-fu so turning to our resident matlab experts for help.
Suppose I run the following code
AA = ones(10,2) + j*ones(10,2)
whos
for i=1:10
AA(i,:) = i + sqrt(i);
end
AA
whos
I get the following output
AA =
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i 1.0000 + 1.0000i
Name Size Bytes Class Attributes
AA 10x2 320 double complex
AA =
2.0000 2.0000
3.4142 3.4142
4.7321 4.7321
6.0000 6.0000
7.2361 7.2361
8.4495 8.4495
9.6458 9.6458
10.8284 10.8284
12.0000 12.0000
13.1623 13.1623
Name Size Bytes Class Attributes
AA 10x2 160 double
i 1x1 8 double
Why is matlab automatically converting the array I specifically requested to be complex to a double?
This is a problem because I have a function that returns values that may or may not be complex and I want to be able to store both.
The exact same behavior happens even when I try AA(i,:) = complex(i + sqrt(i));
Edit: I should have specified that if the first call to my function returns a real value then what happens above happens as well. But if the first return of the function is complex (irregardless of what the rest of the loop returns) the array will stay complex..