Fa ra
Fa ra

Reputation: 3

Moving rows of matrix

I have a matrix like this, I want to move some rows among Matrix

Matrix = 
[ 1   101   201   301
  2   102   202   302
  3   103   203   303
  4   104   204   304
  5   105   205   305
  6   106   206   306
  7   107   207   307
  8   108   208   308
  9   109   209   309
  10   110   210   310];

for example, I want to move row number 6 after row number 2

Matrix = 
         [ 1   101   201   301
           2   102   202   302
           6   106   206   306
           3   103   203   303
           4   104   204   304
           5   105   205   305
           7   107   207   307
           8   108   208   308
           9   109   209   309
           10   110   210   310];

then I want to move row 9 after row 5

 Matrix = 
         [ 1   101   201   301
           2   102   202   302
           6   106   206   306
           3   103   203   303
           4   104   204   304
           5   105   205   305
           9   109   209   309
           7   107   207   307
           8   108   208   308
           10   110   210   310];  

How can I accomplish this in Matlab?

Upvotes: 0

Views: 286

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

Just by indexing:

Matrix = ...
[ 1   101   201   301
  2   102   202   302
  3   103   203   303
  4   104   204   304
  5   105   205   305
  6   106   206   306
  7   107   207   307
  8   108   208   308
  9   109   209   309
  10   110   210   310];

newOrder = [1 2 6 3 4 5 9 7 8 10];
out = Matrix(newOrder,:)

out =

     1   101   201   301
     2   102   202   302
     6   106   206   306
     3   103   203   303
     4   104   204   304
     5   105   205   305
     9   109   209   309
     7   107   207   307
     8   108   208   308
    10   110   210   310

Upvotes: 3

Related Questions