MATLAB Answers

column operator erases complex property

2 views (last 30 days)
Bruno Luong
Bruno Luong on 18 Oct 2020
Commented: Jan on 28 Jul 2021
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
  17 Comments
Bruno Luong
Bruno Luong on 19 Oct 2020
@Vanishta, you miss my point, the COMPLEX command allocates real+imaginary internally, even if imagnary part is 0. That is user intention of using complex command.
The whole purpose of the TIP part of the document is actually tells that user is allowed to create complex array with 0s value in imaginary part.
I expect (:) to preserve that allocation (and keep the data pointer constant) as with RESHAPE. Which is NOT the case.
Why doing such thing, you might ask? Because if I do change a subset of elements of the initial array, it will be faster (assumming array are not shared)
>> format debug
>> z=complex(1:3,0)
z =
Structure address = 2258b2d6a40
m = 1
n = 3
pr = 225a25023c0
1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 + 0.0000i
>> z(3)=1i % I might do this kind of assigment in for-loop
z =
Structure address = 2258b2d6a40
m = 1
n = 3
pr = 225a25023c0
1.0000 + 0.0000i 2.0000 + 0.0000i 0.0000 + 1.0000i
Unfortunately (:) operator would erase my allocation with COMPLEX.

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