在Matlab中对矩阵的每一行中的离散元素进行重新排序 [英] Reordering discrete elements in each row of a matrix in Matlab

查看:171
本文介绍了在Matlab中对矩阵的每一行中的离散元素进行重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中有三个维度为MxN的矩阵X1X2G.我要按如下所述对两者的每一行进行排序

I have three matrices X1, X2, and G in Matlab of the same dimension MxN. I want to order each row of both as described below

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的情况提供了一种算法.您能帮我扩展到我的案子吗?

The code here provides an algorithm for the case without X2. Could you help me to extend it to my case?

推荐答案

在这里您可以执行此操作.我使用了与您的参考代码相同的方法,但没有X2:

Here's how you can do this. I used the same approach as your reference code without 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

这篇关于在Matlab中对矩阵的每一行中的离散元素进行重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆