2014-01-15 15 views
3

您好我有一個4×4矩陣雙如何將值分組(按複平面中的象限)?

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 

我如何將它們組合成不同的組中的1 + 1I,1-1i,-1 + 1I,在MATLAB -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

回答

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) 

根據需要更改順序。你當然也可以從下面使用循環/ cellfun。


或用自動順序:

要知道,我已經根據高斯複平面上選擇一個有意義的順序,因爲你提出了一個並不在數學方面的意義/ phyisics。

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個邏輯比較。它真的更快嗎?

+0

好,'angle'涉及的'atan',並且沒有內置。使用具有匿名函數的'cellfun'可能會很慢。你是對的,你的實現應該更快:)但是我的符號更接近於你如何在數學上下文中編寫東西,通過自我文檔提高可讀性,但是這樣做的好處可以被辯論,因爲它們更多問題的味道。 –

+0

@RodyOldenhuis這是正確的,你的符號可以從一個數學公式中複製出來,並帶有一些大括號。 :p事情是你做了8次,我的強力方法(類似於你的)只做了4次,沒有額外的'&'操作。我不知道運營商'<,>,==和性能方面是否存在差異,但是您似乎有更多的關係操作。 +1無論如何爲你的解決方案,這只是一個品味問題;) – thewaywewalk

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) 
};