2016-10-20 247 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。我认为从更大的一个选择一个子矩阵有更好的方法,但现在不能想到它。

相关问题