Reputation: 13
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 :)
Upvotes: 1
Views: 225
Reputation: 1860
To rearrange or select any desired rows:
B = A(V,:);
The same concept could be used for columns and for rearranging, selecting or repeating any desired row or column:
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
Learn about colon operator(:) here:
http://www.mathworks.com/help/matlab/ref/colon.html
Upvotes: 3