You can do this with isreal . Turning off isreal does not give vector output, which is strange for MATLAB, as it usually does. So you need to use a for loop.
arr = [1+i 5 6-3i 8]; arrReal = []; for idx = 1:numel(arr) if isreal(arr(idx)) arrReal(end+1) = arr(idx); end end
I believe that great people will come to a decision without a loop.
Shay edit:
Version with preliminary distribution of the output
arrReal = NaN( size(arr) ); % pre-allocation for idx = 1:numel(arr) if isreal( arr(idx) ) arrReal(idx) = arr(idx); end end arrReal( isnan( arrReal ) ) = []; % discard non-relevant entries
Of course, this goal can be achieved without cycles (see other answers). But for this loop version, preselection is an important component.
Hebelehododo
source share