select complex numbers from cell array based on the real part

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

댓글 수: 2

Why are you storing scalar numerics in a cell array?
Its the solution of the eigenproblem. I convert it later to matrix.

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

 채택된 답변

dpb
dpb 2020년 6월 3일
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

i want to find the corresponding imaginary parts to the maximum real values of the complex numbers as well.
The maxVal that dpb's code returns will be complex values. You can imag() them.
The problem with the maxVal is that keeps the complex number with higher abs value either its real or imaginary.
It returns complex number but in the example showed above i want the corresponding imaginary part of the highest real part, and it returns the complex number with the higher value of imaginary part.
The one that i want to extract is
val(:,:,14,1,1,1,1)
[7.5646e+02 + 1.4443e+03i]
and it gives me back
val(:,:,15,1,1,1,1) =
{[3.4384e+01 + 1.6918e+03i]}
which has the absolute highest imaginary part.
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.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2020년 6월 3일

댓글:

dpb
2020년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by