How can I sort a 2-D array in MATLAB with respect to one column?
I think the sortrows function is what you're looking for.
>> sortrows(data,1)
ans =
-1 4
1 3
5 7
An alternative to sortrows()
, which can be applied to broader scenarios.
-
save the sorting indices of the row/column you want to order by:
[~,idx]=sort(data(:,1));
-
reorder all the rows/columns according to the previous sorted indices
data=data(idx,:)