嗨我有一个4x4双矩阵
1+2i 2-1i -3-2i -1+4i
3-1i -3+2i 1-3i -1-3i
4+3i 3+5i 1-2i -1-4i
4+2i -5-2i 2+3i 2-1i
如何在MATLAB中将它们分组到1 + 1i,1-1i,-1 + 1i,-1-1i类别的不同组中?
E.g。 1 + 1i组:1 + 2i,4 + 3i,3 + 5i,4 + 2i,2 + 3i 1-1i组:2-1i,3-1i,1-3i,1-2i,2-1i -1 + 1i组:-1 + 4i,-3 + 2i -1-1i基团:-3-2i,-1-3i,-1-4i,-5-2i
答案 0 :(得分:4)
怎么样:
% example matrix
Z = [ 1+2i 2-1i -3-2i -1+4i ;
3-1i -3+2i 1-3i -1-3i ;
4+3i 3+5i 1-2i -1-4i ;
4+2i -5-2i 2+3i 2-1i ]
蛮力逼近,如果您不关心群组的顺序:
classification = ceil( angle(Z)*4/(2*pi) ) + 2
quadrant1 = Z(classification == 3)
quadrant2 = Z(classification == 4)
quadrant3 = Z(classification == 1)
quadrant4 = Z(classification == 2)
根据需要更改顺序。你当然也可以使用下面的loop / cellfun。
或自动下单:
请注意,我根据高斯复平面选择了一个有意义的顺序,因为你提出的那个在数学/物理学方面没有意义。
classification = ceil( angle(Z)*4/(2*pi) );
classification(classification < 1) = classification(classification < 1) + 4;
for ii = 1:4;
quadrant{ii,:} = Z(classification == ii);
end
或用以下代码替换循环:
quadrant = cellfun(@(x) Z(classification == x), num2cell(1:4),'Uni',0)'
返回一个包含4个单元格的单元格数组,每个象限按顺序包含所有相应的复数:
>> quadrant{:}
ans =
1.0000 + 2.0000i
4.0000 + 3.0000i
4.0000 + 2.0000i
3.0000 + 5.0000i
2.0000 + 3.0000i
ans =
-3.0000 + 2.0000i
-1.0000 + 4.0000i
ans =
-5.0000 - 2.0000i
-3.0000 - 2.0000i
-1.0000 - 3.0000i
-1.0000 - 4.0000i
ans =
3.0000 - 1.0000i
2.0000 - 1.0000i
1.0000 - 3.0000i
1.0000 - 2.0000i
2.0000 - 1.0000i
Don't punch me Rody - 但我宁愿按如下方式写出来:
Im = imag(Z) >= 0;
Re = real(Z) >= 0;
quadrant = {
Z( Re & Im)
Z( ~Re & Im)
Z( ~Re & ~Im)
Z( Re & ~Im)
};
...因为我打算避免所有这8次逻辑比较。真的更快吗?
答案 1 :(得分:3)
最快,最直观的恕我直言:
Im = imag(Z);
Re = real(Z);
quadrant = {
Z(Im > 0 & Re > 0)
Z(Im > 0 & Re <= 0)
Z(Im <= 0 & Re > 0)
Z(Im <= 0 & Re <= 0)
};