在Matlab

时间:2017-07-24 17:38:25

标签: matlab

我在相同维度X1的Matlab中有三个矩阵X2GMxN。我想按如下所述订购两者的每一行

clear all
rng default;
M=12;
N=3;
X1=randi([0 1], M,N); %binary values JUST for simplicity
X2=randi([0 1], M,N);
G=randi([0 1], M,N);


%for i=1,...N
%    List in descending order the elements of G(i,:)

%    If G(i,h)=G(i,j), then order first G(i,h) if X1(i,h)>X1(i,j), and  
%    order first G(i,j) if X1(i,j)>X1(i,h). 

%    If G(i,h)=G(i,j) and X1(i,h)=X1(i,j), then order first G(i,h) if 
     X2(i,h)>X2(i,j), and order first G(i,j) if X2(i,j)>X2(i,h). 

%    If G(i,h)=G(i,j), X1(i,j)=X1(i,h), and X2(i,j)=X2(i,h), then any 
     order is fine. 

%    Use the order determined for G(i,:) to order X1(i,:) and X2(i,:).
%    Create B(i,:)=[X1(i,:) X2(i,:) G(i,:)]. 
%end

实施例

X1=[0 0 0 1;
    1 1 0 0];

X2=[0 1 1 0;
    0 1 0 0];

G=[0 1 0 1;
   0 0 1 0];

B=[1 0 0 0 | 0 1 1 0 | 1 1 0 0; 
   0 1 1 0 | 0 1 0 0 | 1 0 0 0];

代码here为没有X2的案例提供了算法。你能帮我把它扩展到我的案子吗?

1 个答案:

答案 0 :(得分:1)

以下是如何做到这一点的。我使用的方法与没有X2的参考代码相同:

clear
rng default; %--> only for testing
% Set up random matrices
M=8000;
N=20;    

X1o = randi([0 1], M,N);
X2o = randi([0 1], M,N);
G = randi([0 1], M,N);

% Initial order of the indices of all matrices
i=repmat((1:N), M,1);

% Sort rows of X2 in descending order, store sorting indices in iX2. Only
% the indices are used.
[~,iX2] = sort(X2o,2,'descend');

% The indices iX will be relative to each row. We need these indices to be
% offset by the number of elements in a column, so that the indices refer 
% to each specific cell in the matrix. 
ofsts = 1 + repmat((0:M-1)', 1, N);

% Sort X1 according to the sorted version of X2
X1 = X1o((iX2-1)*M + ofsts);

% In the code with only one X-matrix, X only gets sorted using 
% the "sort()" function. In this situation, X1 gets sorted twice: 
% once according to X2 and than in descending mode. The latter is done 
% automatically via de "sort()" function but the indices need to be sorted 
% according to X2 as well.
iX1 = i((iX2-1)*M + ofsts); 

% Sort rows of the sorted version of X1. Only the indices are needed to
% sort the iX1 from the previous sorting action.
[~,ii] = sort(X1,2,'descend');
iX1 = iX1((ii-1)*M + ofsts) ;

% Reorder G to be sorted the same as X1. The indices are sorted separately
% because the sorting X1o and X2o is done using the original values, not
% the sorted versions, as done in the example code with only one X-matrix.
G = G((iX1-1)*M + ofsts);
iG = i((iX1-1)*M + ofsts);

% Sort rows of G in descending order and store the sorting indices iG.
[G,ii] = sort(G,2,'descend');
iG = iG((ii-1)*M + ofsts);

% Sort the orginal X1o and X2o in the same way as G
X1 = X1o((iG-1)*M + ofsts);
X2 = X2o((iG-1)*M + ofsts);

B = [X1 X2 G]

示例

输入:

X1 =  3     1     2     3
      3     1     1     2


X2 =  3     1     3     2
      3     1     2     1


G  =  2     3     2     1
      3     1     1     3

输出:

B =

  1     3     2     3   |   1     3     3     2   |   3     2     2     1
  3     2     1     1   |   3     1     2     1   |   3     3     1     1