【发布时间】:2017-03-23 13:56:51
【问题描述】:
我在 Matlab 中有两个矩阵 X 和 G 相同维度 MxN。我想按如下所述订购两者的每一行
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
这段代码做我想做的事
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=8000 和N=20 大约需要 0.6 秒,因为我必须多次重复该过程。
你有更有效的建议吗?
例子
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];
【问题讨论】:
-
如果您发布一个带有输入和输出的小示例会有所帮助,以明确您想要什么
-
我已经添加和示例
标签: matlab