column operator erases complex property

1 view (last 30 days)
Why column (:) changes my data? (R2020b)
>> z=complex(3,0)
z =
3.000000000000000 + 0.000000000000000i
>> isreal(z)
ans =
logical
0
>> isreal(reshape(z,[],1))
ans =
logical
0
>> isreal(z(:)) %%%% <= only column returns 1
ans =
logical
1
  21 Comments
Bruno Luong
Bruno Luong on 28 Mar 2022
It seems
  • sortrows works on complex input then decide to "cast" the sorted result to real.
  • sort(z) does not post cast.
  • Whereas sort(z(:)) cast the input first.
Walter Roberson
Walter Roberson on 29 Mar 2022
The part I was forgetting was this from sort:
  • If A is complex, then by default, sort sorts the elements by magnitude. If more than one element has equal magnitude, then the elements are sorted by phase angle on the interval (−π, π].
But these days there is a 'ComparisonMethod' option, of 'real' or 'magnitude'

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Oct 2020
For reasons I do not understand, z(:) is being treated as an expression. If you make z larger but complex, then reshape(z,[],1) keeps the same data pointer, but z(:) creates a new data pointer each time -- which is not the case if z is not complex.
I have two speculations at the moment:
  1. Hypothetically, since array indexing is treated as an expression, Mathworks might have wanted consistency around dropping the complex part of expressions when the complex part was all zero. This explanation is a bit weak as it does not explain why they did not treat reshape() the same way, and does not explain why scalar z keeps the same data pointer (but non-scalar z does not.)
  2. Hypothetically, it might have to do with the change to representation of complex in R2018a. This explanation is a bit weak as it does not explain why they did not treat reshape() the same way, and does not explain why scalar z keeps the same data pointer (but non-scalar z does not.) On the other hand, this hypothesis has the merit that it would be testable by going back to R2017b and seeing if (:) had the same behaviour there.
  7 Comments
Bruno Luong
Bruno Luong on 28 Jul 2021
How to ask TMW a behavior that is not documented nor a bug?
Jan
Jan on 28 Jul 2021
@Bruno Luong: You are Bruno. Just write them an email and ask for an explanation. From time to a developper has called by by phone to explain details concerning a discussion in the forum. They are interested in active users.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!