在MATLAB中对具有虚部的复杂数组元素及其进行排序 [英] Sorting the complex array elements with imaginary part along with it in MATLAB

查看:156
本文介绍了在MATLAB中对具有虚部的复杂数组元素及其进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据点,它们是复数,比如说8行6列.如果看一下程序,您会看到数据集是非常随机地排列的.第一,第二,第三列可以,但是第四列有点反常.我的意思是说,在B(5,4)处的元素之后(即25.9868674011374),它将到达2.74257567017122 [B(6,4)],而不是26.8410063269595,后者已到达B(6,6).我希望该列以升序排列(对于实际部分,应进行所有排序).此外,在第5列中,即使实部ae以升序排列,虚部也被交换.例如,在B(2,5)之后,它应该是B(3,6)而不是B(3,5).我已经尝试过排序"命令,但是它不能按我的意愿工作.

I have a set of data points, which are complex numbers which have, let’s say, 8 rows and 6 columns. If you look at the program, you can see that data sets are arranged very randomly. 1st, 2nd, 3rd columns are okay, but 4th column, is bit anomalous. What I mean to say is that after element at B(5, 4) (i.e. 25.9868674011374) it is coming 2.74257567017122 [B(6, 4)] instead of 26.8410063269595 which has gone to B(6, 6). I want that column arranged in ascending order (every sorting should be done for real part). Also, in the 5th column, even though the real parts ae arranged in ascending order, the imaginary part are kind of swapped. For example, after B(2,5) it should be B(3,6) not B(3,5). I have tried ‘sort’ command, but it not working as I want.

对于那些想知道排序问题的人,只需尝试运行以下命令:

For the people who is wondering wht is the problem with sort, just try to run this command:

A = [1+2i 3+i i 0 -i]; Dreal= sort(real(A)); Dimg = sort(imag(A));D=[Dreal;Dimg];

我想要的是-i,0,i,3 + I,1 + 2i,那种给的是其他东西.

What I want is -i, 0, i ,3+I,1+2i, what sort gives is something else.

此外,在某些情况下,第4列,第5列和第6列的行为可能正常",但第1列,第2列,第3列的行为却不稳定.因此,即使第4列,第5列和第6列的状态为正常",我也希望按升序对它进行排序.有什么办法吗?顺便说一句,我正在使用Matlab- 2015b.代码是:

Also, there may be instances where 4th, 5th and 6th column may behave ‘normally’, but 1st, 2nd, 3rd columns behaving erratically. So even if 4th, 5th and 6th columns are behaving ‘normally’ I want it to be sorted down in ascending order. Any way through this? By the by, I am using Matlab- 2015b. The code is:

clear all; clc;
B=[-2.14981736484179 + 0.00000000000000i,-1.38134547606946 + 0.00000000000000i,1.38451324569297 + 0.00000000000000i,22.5759136576435 + 0.00000000000000i,2.28536796878740 + 0.333911501246080i,2.28536796878740 - 0.333911501246080i;-2.22047322414157 + 0.00000000000000i,-1.43596350944258 + 0.00000000000000i,1.43889226552228 + 0.00000000000000i,23.4278498788255 + 0.00000000000000i,2.39484729461819 + 0.303429715954385i,2.39484729461819 - 0.303429715954385i;-2.29148887606605 + 0.00000000000000i,-1.49057388951113 + 0.00000000000000i,1.49328382360683 + 0.00000000000000i,24.2803021611395 + 0.00000000000000i,2.50423839041542 - 0.265225265037282i,2.50423839041542 + 0.265225265037282i;-2.36284412024645 + 0.00000000000000i,-1.54517861014711 + 0.00000000000000i,1.54768832224205 + 0.00000000000000i,25.1333019698605 + 0.00000000000000i,2.61351621914550 + 0.215386193278572i,2.61351621914550 - 0.215386193278572i;-2.43451884781340 + 0.00000000000000i,-1.59977935450756 + 0.00000000000000i,1.60210590260749 + 0.00000000000000i,25.9868674011374 + 0.00000000000000i,2.72266244928806 - 0.142395604889199i,2.72266244928806 + 0.142395604889199i;-2.50649346218904 + 0.00000000000000i,-1.65437753673930 + 0.00000000000000i,1.65653651325774 + 0.00000000000000i,2.74257567017122 + 0.00000000000000i,2.92075248853987 + 0.00000000000000i,26.8410063269595 + 0.00000000000000i;-2.57874914363635 + 0.00000000000000i,-1.70897433922564 + 0.00000000000000i,1.71097996266870 + 0.00000000000000i,2.74509085049068 + 0.00000000000000i,3.13593380433481 + 0.00000000000000i,27.6957188653678 + 0.00000000000000i;-2.65126800477853 + 0.00000000000000i,-1.76357074549849 + 0.00000000000000i,1.76543595943333 + 0.00000000000000i,2.78382099280761 + 0.00000000000000i,3.31458248082037 + 0.00000000000000i,28.5509993172157 + 0.00000000000000i];
k=1:1:4;
B1=sort(real(B));
B2=sort(imag(B));
B3=sort(B);

谢谢.

推荐答案

您似乎想要sort(A,'ComparisonMethod','real')功能

You seem to be wanting the sort(A,'ComparisonMethod','real') feature available in Matlab r2017a, but for use in r2015b.

您可以使用sortrows函数重新创建功能:

You can recreate the feature using the sortrows function:

clear
A = [1+2i 3+1i 1i 0 -1i];
AB = [real(A); imag(A)];
[~,I] = sortrows(AB');
Asort = A(I)

产量Asort = [0 - 1i 0 + 0i 0 + 1i 1 + 2i 3 + 1i]. sortrows首先根据第一列对数组的每一行进行排序,然后断开与下一列的联系,依此类推,直到对整个数组进行排序为止(文档

yields Asort = [0 - 1i 0 + 0i 0 + 1i 1 + 2i 3 + 1i]. sortrows sorts each row of an array first according to the first column, then breaks ties with the next column and so on until the whole array is sorted (Documentation here). The idea here is to make the first column the real component, and the second column the imaginary component.

对于复数的高维数组,您可以选择一些方法,但是它们都将使用与上述相同的原理.如果要对矩阵B的所有元素进行排序,则可以使用以下代码生成排序后的向量:

For higher-dimensional arrays of complex numbers, you have a few options, but they'll all use the same principle as above. If you want to sort all elements of your matrix B, you can generate a sorted vector use the following code:

Bsep = [real(B(:)),imag(B(:))];
[~,I] = sortrows(Bsep);
SortedVec = B(I);

要重塑为8x6数组(排序后的元素从左到右按列排列),只需添加以下行即可:

To reshape back into an 8x6 array (with sorted elements arranged down columns, from left to right), simply add this line:

Bsort = reshape(SortedVec,8,6);

或者,也许您想对数组的每一列分别进行排序.只需在循环中使用sortrows:

Or, perhaps you want to sort each column of your array separately. Just use sortrows in a loop:

nCols = size(B,2);
Bsort = NaN(size(B));
for j = 1:nCols
   Bsep = [real(B(:,j)),imag(B(:,j))];
   [~,I] = sortrows(Bsep);
   Bsort(:,j) = B(I,j);
end

如果您想先按虚数部分 进行排序,然后与 real 部分断开联系,那么您要做的就是将调用的位置切换到上面任何代码段中的realimag.例如,

If you instead want to sort by the imaginary part first, then break ties with the real part, all you need to do is switch the position of the calls to real and imag in any of the code snippets above. For example,

clear
A = [1+2i 3+1i 1i 0 -1i];
AB = [imag(A); real(A)]; % note that real and imag are switched
[~,I] = sortrows(AB');
Asort = A(I)

产量Asort = [0 - 1i 0 + 0i 0 + 1i 3 + 1i 1 + 2i]

这篇关于在MATLAB中对具有虚部的复杂数组元素及其进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆