2016-10-20 245 views
0

我有一個4×4矩陣像X和一個2×2矩陣狀A.比較相應的塊矩陣元素中的另一矩陣

X= x11 x21 x31 x41   A= 1 0 
    x12 x22 x32 x42    0 1 
    x13 x23 x33 x43 
    x14 x24 x34 x44 

我分X四個2×2塊用下面的代碼:

Y=X; 
    sx=size(X); 
    mask=logical([1 1;1 1]); 
    for i=1:2:sx(1) 
     for j=1:2:sx(2) 
     px=X(i:i+1,j:j+1); 
    end 
end 

現在我要矩陣A的每個元素與矩陣X的相應塊比較

If the first element of matrix A is zero, then x11 should be lower than x22. 
if not, I should swap them with each other. 


If the first element of matrix A is one ,then x11 should be greater than x22. 
if not,I should swap them with each other. 

回答

0

我想你應該可以在你的循環中加入這段代碼。

if A(1,1)==0 & px(2,2)>px(1,1) 
    [px(1,1), px(2,2)] = deal(px(2,2),px(1,1)); 
elseif A(1,1)==1 & px(2,2)<px(1,1) 
    [px(1,1), px(2,2)] = deal(px(2,2),px(1,1)); 
end 

PS。我認爲從更大的一個選擇一個子矩陣有更好的方法,但現在不能想到它。

相關問題