select complex numbers from cell array based on the real part
4 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/309491/image.jpeg)
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 件のコメント
採用された回答
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 件のコメント
dpb
2020 年 6 月 3 日
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 件)
参考
カテゴリ
Help Center および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!