How to avoid automatic conversion of complex to real?

조회 수: 5 (최근 30일)
Lars Hansson
Lars Hansson 2023년 3월 23일
답변: Bruno Luong 2023년 3월 23일
I've encountered many difficult-to-find bugs due to how Matlab silently and automatically converts complex arrays to real, similar to this pattern:
% create complex array A
A = [0 + 0i, 0 + 1i];
% check if real
isreal(A)
ans = logical
0
% create complex scalar x
x = complex(0 + 0i);
% check if real
isreal(x)
ans = logical
0
% replace one value in A (complex) with x (complex)
A(2) = x;
isreal(A)
ans = logical
1
% A is now real
Is there any way to avoid this automatic lowering to real (when encountering complex zeros)?
Since for large A it is unnecessarily expensive to have Matlab convert it to real, only for you to have to convert it back to complex.
I have tried to get support from Mathworks (as a service request), but they only say it is working as intended.
  댓글 수: 1
Lars Hansson
Lars Hansson 2023년 3월 23일
It makes no sense, since if you want to reduce the memory usage, why not convert all the way down to int8 or int1?The values are all zero after all :P

댓글을 달려면 로그인하십시오.

답변 (1개)

Bruno Luong
Bruno Luong 2023년 3월 23일
Yes it works as intended, and no there is no way to disable it.
This automatic conversion also affects the CPU and sometime it is very unexpected. We have discussed this in the pass.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by