Reputation: 73
I have matrix with n
lines and m
columns, suppose the following
[5 6 2 6 8
7 6 9 0 4
8 0 9 5 4]
I want to generate a matrix in which, every line is sorted in ascending order, for example, vector (1,m)
, vector (2,m)
, vector (3,m)
, and so on:
[2 5 6 6 8
0 4 6 7 9
0 4 5 8 9]
How can I do this? Do I have to use a loop to achieve this?
Upvotes: 0
Views: 337
Reputation: 112759
No need for loops. sort
automatically works in a vectorized manner. By default it sorts the input array along the first non-singleton dimension. To sort each row you need to specify 2
as an additional input (sort along the second dimension).
So, denoting your matrix as x
, just use
sort(x,2)
Upvotes: 3