select complex numbers from cell array based on the real part

4 views (last 30 days)
Ilias Minas
Ilias Minas on 3 Jun 2020
Commented: dpb on 3 Jun 2020
Hi all,
I have a 7D cell array with complex numbers.
I want to keep the complex number with the highest real part, of all the values of the 3rd parameter (1:15).
For example in this case i want only the val(:,:,14,1,1,1,1) which has the highest real part.
I tried to do it using the following command
maxVal = max(cell2mat(my_array),[],3);
However, it keeps the complex number with higher imaginary part. In this case
val(:,:,15,1,1,1,1)
Any idea of how can i do this?
Thank you

Accepted Answer

dpb
dpb on 3 Jun 2020
Well, if you want the max() of the real part, then you'll have to limit to only looking at the real part -- max() for complex returns max(abs())
tmp=squeeze(cell2mat(my_array));
[maxRealVal,imxR] = max(real(tmp));
maxVal=tmp(imxR);
  4 Comments
dpb
dpb on 3 Jun 2020
With your code, yes...with mine, no. Attach some readable data so don't have to make it up (don't use images, select/past text instead).
Well, what the ... poke, poke, ...
val=complex([0.21,0.28,0.34,745.4,34.3],[1460.9,1460.9,1460.9,1444.3,1691.9]); % approx subset sample data
% find return value with maximum real part...
[vMxR,iMxR]=max(real(val)); % return location
% display result
>> val(iMxR)
ans =
7.4540e+02 + 1.4443e+03i
>> imag(val(iMxR))
ans =
1.4443e+03
>>
by contrast,
>> abs(val)
ans =
1.0e+03 *
1.4609 1.4609 1.4609 1.6253 1.6922
>> [~,imx]=max(abs(val))
imx =
5
>> [~,imx]=max((val))
imx =
5
>>
which shows since the real part is stlll small(ish) in comparison, the larger imaginary component dominates in the abs() value which is what max() returns for complex variable.

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!