1

I 've a specific problem of sorting rows in matlab. This is mine example entry matrix:

 A =

 [0 1 1;
  0 1 2;
  1 0 3;
  1 0 4;
  1 1 5;
  0 1 6;]

and this is "sorting vector"

 V=

  1 
  4 
  6 
  2 
  3 
  5 

How to get an output matrix like this:

B=

 [0 1 1;
  1 0 4;
  0 1 6;
  0 1 2;
  1 0 3;
  1 1 5]

?

First I've added vector V to matrix A (last column) but the next step I don't know how it should look. I'm stuck.

In advance, thanks for your time and help :)

4

2 回答 2

3

要重新排列或选择任何所需的行:

B = A(V,:);

相同的概念可用于列以及重新排列、选择或重复任何所需的行或列:

V2 = [3 1 3];
B2 = A(:,V2);

B2 = 

 1     0     1
 2     0     2
 3     1     3
 4     1     4
 5     1     5
 6     0     6

在此处了解冒号运算符(:):

http://www.mathworks.com/help/matlab/ref/colon.html

于 2013-05-16T23:40:51.873 回答
3

这可能是答案:

B = A(V(:),:);

于 2013-05-16T23:25:17.317 回答