Matlab中矩阵每行离散元素的重新排序

Matlab中矩阵每行离散元素的重新排序,matlab,Matlab,我在Matlab中有三个矩阵X1,X2,和G,它们的维数相同MxN。我想按如下所述订购每一行 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,:)

我在Matlab中有三个矩阵
X1
X2
,和
G
,它们的维数相同
MxN
。我想按如下所述订购每一行

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];

代码为不带
X2
的情况提供了算法。你能帮我把它扩展到我的案例中吗?

以下是你如何做到这一点的。我使用了与您的参考代码相同的方法,没有
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

谢谢您。我可以问你为什么你开始设置
index=cell(2,1)
,然后
index
for
循环之后变成cell
Mx1
?你不能只做
I=repmat((1:1:N),M,1)
?是的,我在测试了一些不同的东西之后忘了更改它。我会编辑它。