column operator erases complex property

1 次查看(过去 30 天)
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 个评论
Bruno Luong
Bruno Luong 2022-3-28
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 2022-3-29
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'

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-10-18
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 个评论
Bruno Luong
Bruno Luong 2021-7-28
How to ask TMW a behavior that is not documented nor a bug?
Jan
Jan 2021-7-28
@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.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by