## How to combine two matrices to one and sort the rows?

on 5 Jun 2012

### Oleg Komarov (view profile)

Hello, my question is given a matrix M, say 10*5 size and one 10*1 say n. I want to make a new matrix that combine those into one matrix say M_new(10*6 size), the first five columns come from M, and 6th column comes from n.

Afterwards, sort the rows from largest to smallest by the last entry, say the number in n. Is there a good code on this? Thanks.

## Products

No products are associated with this question.

### Oleg Komarov (view profile)

on 5 Jun 2012

```M_new = [M, n];
M_new = sortrows(M_new,6);
```

See sortrows() for details.

C Zeng

### C Zeng (view profile)

on 5 Jun 2012

Thanks, or M_new = sortrows(M_new,-6), right? :-)

Oleg Komarov

### Oleg Komarov (view profile)

on 5 Jun 2012

Yes, I always forget about the minus!!

C Zeng

### C Zeng (view profile)

on 6 Jun 2012

Haha, I just found it.

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Apply Today

New to MATLAB?

Learn MATLAB today!