Is there a fast mechanism to interleave the real and imaginary parts of a large array of complex numbers?

6 views (last 30 days)
Which is the fastest way to convert a very large array of complex numbers in the following format :
[ real1 + img1;
  real2 + img2;
  ....
]
 to the following format:
[real1 img1 real2 img2 .....]
Input Data:
>> N = 20971520;
>> a = randn(1,N)+1i*randn(1,N);
>> b = zeros(1, 2*numel(a));
 

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 6 Apr 2017
The following are a few of the possible and fast solutions:
1)  b(1:2:end) = real(a);
     b(2:2:end) = imag(a);
This is the slowest of the 3 approaches as it involves indexing.
2) c = ([real(a);imag(a)]); c = c(:);
This approach uses linear indexing and is faster than 1) but slightly slower than 3)
3) c = reshape([real(a);imag(a)],[],1);
This approach appears to be the fastest of the 3 because it involving only data shuffling and no indexing.
There are other solutions involving logical indexing and using 'kron' to interleave data but they are slower than the 3 suggested solutions.
  1 Comment
Bruno Luong
Bruno Luong on 27 Mar 2023
Edited: Bruno Luong on 27 Mar 2023
@Ian Atkinson you must misstype semincolon ";" with comma ","; and to use 2) and 3) your a vector must be row vector not column vector.
NOTE that the expression
[ real1 + img1;
real2 + img2;
....
]
written in the question is wrong it also mix up between comma and semicolon, not counting there must be a missing i or j somewhere.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!