MATLAB: Sorting from highest to lowest for a particular column

sorting

I have A (3*5) matrix. I have B(14*6) matrix.
Now I want C (14*5) matrix from A and B in such a way that column 2 and 3 for each row of matrix B will be replaced by corresponding cell value of A.
For example – column 2 and 3 of 1st row of B is 1 and 4. Then we need to find the value of A(1,4) which is 10. This 10 will be second column value for 1st row of matrix C.
C(:,1)= B(:,1) ; C(:,2) = from above condition; C(:,3) = B(:,4); C(:,4) = B(:,5) ; C(:,5) = B(:,6)
Eventually, I want matrix D(14*5) from C with the second column value sorted from highest to lowest. Then all the rows in column 1, 3, 4, and 5 will be according to corresponding second column.
Can anyone please help me how to get this D matrix from A and B? I have attached A, B, C, and D matrix here for the clarification.
My B matrix row number is much larger in real case. I just made it smaller here to simplify the problem. Matrix A (3*5) is fixed.
Thanks in advance.
A=[13 12 11 10 3
13 9 8 7 2
13 6 5 4 1];
B=[1 1 4 0 3 2
2 1 2 2 0 2
3 1 2 0 5 0
4 2 2 3 2 0
5 3 5 3 0 2
6 3 4 0 0 5
7 2 5 3 2 0
8 2 4 0 3 0
9 2 3 0 2 3
10 3 2 0 3 2
11 3 3 0 0 5
12 1 3 0 5 0
13 1 1 1 2 2
14 1 5 0 2 2];
C=[1 10 0 3 2
2 12 2 0 2
3 12 0 5 0
4 9 3 2 0
5 1 3 0 2
6 4 0 0 5
7 2 3 2 0
8 7 0 3 0
9 8 0 2 3
10 6 0 3 2
11 5 0 0 5
12 11 0 5 0
13 13 1 2 2
14 3 0 2 2];
D=[13 13 1 2 2
2 12 2 0 2
3 12 0 5 0
12 11 0 5 0
1 10 0 3 2
4 9 3 2 0
9 8 0 2 3
8 7 0 3 0
10 6 0 3 2
11 5 0 0 5
6 4 0 0 5
14 3 0 2 2
7 2 3 2 0
5 1 3 0 2];

Best Answer

Simpler and no loops required with sub2ind:
>> X = sub2ind(size(A),B(:,2),B(:,3));
>> C = [B(:,1),A(X),B(:,4:end)]
C =
1 10 0 3 2
2 12 2 0 2
3 12 0 5 0
4 9 3 2 0
5 1 3 0 2
6 4 0 0 5
7 2 3 2 0
8 7 0 3 0
9 8 0 2 3
10 6 0 3 2
11 5 0 0 5
12 11 0 5 0
13 13 1 2 2
14 3 0 2 2
>> D = sortrows(C,-2)
D =
13 13 1 2 2
2 12 2 0 2
3 12 0 5 0
12 11 0 5 0
1 10 0 3 2
4 9 3 2 0
9 8 0 2 3
8 7 0 3 0
10 6 0 3 2
11 5 0 0 5
6 4 0 0 5
14 3 0 2 2
7 2 3 2 0
5 1 3 0 2