0

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..

arynaq
  • 6,710
  • 9
  • 44
  • 74
  • It is a duplicate but there are no resolutions in the linked answers, a link at the bottom of the answers leads to a question that hints at this being a matlab bug but I cannot find anything official. I have found a dirty hack to fix my specific problem here https://gist.github.com/anonymous/da7d511871c17d55a0c5935717fddd01 but this cannot be true, there cant be that few users that require indexing on complex matrices where the first indexing MIGHT be on a 0 imaginary part. – arynaq Oct 25 '16 at 20:27
  • If none of the values in the vector/matrix an imaginary part then they are not complex thus matlab decides to store it as a double. In you gist you do not even need to cast them into complex as A(1,:) already is, so the whole matrix will remain complex. – mpaskov Oct 25 '16 at 20:52
  • @arynaq Why do you need the complex attribute to say there? MATLAB will automatically toggle between complex and not and this won't affect any operations that you may perform. – Suever Oct 26 '16 at 00:17
  • irregardless is not a word. – rayryeng Oct 26 '16 at 05:54
  • Also, cast the entire array `AA` to be complex after your operations, not each row individually. Also, as with the other post, there's no need to explicitly force it to be complex when it's sufficient to be real. MATLAB will change the type based on the data. You're making an issue over nothing. – rayryeng Oct 26 '16 at 06:50

1 Answers1

0

Because you are fill it with non complex values it by reusing the variable, try this:

AA = ones(10,2) + j*ones(10,2)
whos

for i=1:10
    AA(i,2) = i + sqrt(i);
end
AA
whos
mpaskov
  • 1,234
  • 2
  • 11
  • 21
  • He doesn't actually over-write it, he just fills it in with non-complex values. – Suever Oct 25 '16 at 20:12
  • @Suever, I kind of meant overwriting the complex values in it, but I can see that it can be confusing, so i changed the wording, Thanks. – mpaskov Oct 25 '16 at 20:16