在Matlab中重新排列矩阵每一行中的元素 [英] Reordering the elements in each row of a matrix in Matlab

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

问题描述

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

I have two matrices X 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;
X=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 X(i,h)>X(i,j), and  
%    order first G(i,j) if X(i,j)>X(i,h). If G(i,h)=G(i,j) and       
%    X(i,j)=X(i,h), then any order is fine. 
%    Use the order determined for G(i,:) to order X(i,:).
%    Combine the ordered X(i,:) and G(i,:) in B(i,:)
%end

这段代码可以满足我的要求

This code does what I want

A(:,:,1)=X;
A(:,:,2)=G;       
B=zeros (size(A,1),2*N); 
for i = 1:size(A,1),
    B(i,:) = reshape(sortrows(squeeze(A(i,:,:)), [-2 -1]),1,2*N);
end

但是,当M很大时,它可能会变慢.例如,使用M=8000N=20大约需要0.6秒,这与我不得不重复多次该过程有关.

However it may slow when M is large. For example, with M=8000 and N=20 it takes approx 0.6 sec, which is a lot as I have to repeat the procedure several times.

您有更有效的建议吗?

示例

X=[0 0 0 1;
   1 1 0 0];

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

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

推荐答案

请参见下面的注释代码,该代码复制了代码的结果.它使用sort两次,包括排序索引输出.如果G中的值相等,则是第一次确定您描述的抢七局情况.第二次是根据G排序.

See the below commented code which reproduces your code's results. It uses sort, including the sorting indices output, twice. The first time is to decide the tie-break situation you described if values in G are equal. The second time is to sort according to G.

在我的PC上,它以8000x20大小的矩阵运行,大约需要0.017秒.

On my PC, it runs with matrices of size 8000x20 in around 0.017 secs.

clear
rng default;
% Set up random matrices
M=8000;
N=20;
X=randi([0 1], M,N);
G=randi([0 1], M,N);
tic;

% Method: sort X first to pre-decide tie-breakers. Then sort G. Then merge.

% Sort rows of X in descending order, store sorting indices in iX
[X,iX] = sort(X,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. (See below for example).
ofsts = 1 + repmat((0:M-1)', 1, N);
% Reorder G to be sorted the same as X was.
G = G((iX-1)*M + ofsts);
% Sort rows of G in descending order and store the sorting indices iG.
[G,iG] = sort(G,2,'descend');
% Reorder X to be sorted the same as G.
X = X((iG-1)*M + ofsts);
% Merge the two matrices
B = [X, G];

toc;
% Elapsed time < .02 secs for 8000x20 matrices.


第一张图显示了示例矩阵iX,以说明索引在每一行中如何相对.第二张图片显示iX+ofsts,以说明其给出的绝对矩阵元素数,请注意它们都是唯一的!

This first image shows an example matrix iX, to illustrate how the indices are just relative within each row. The second image shows iX+ofsts to illustrate the absolute matrix element number it gives, note they are all unique!

iX

iX+ofsts

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

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