Given the following 4x6 matrix A:
A = [
10 11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27
28 29 30 31 32 33
];
and two variables containing the row and column subscripts (r and c, respectively):
r = [
1 1 1
1 1 1
3 3 3
3 3 3
4 4 4
];
c = [
1 3 6
1 2 4
1 2 6
1 4 5
1 5 6
];
I want to create a new 5x3 matrix B that takes each row and column combination from r and c such that:
B = [
10 12 15
10 11 13
22 23 27
22 25 26
28 32 33
];
Note that element B(1,1) is derived by using r(1,1) as the row subscript and c(1,1) as the column subscript. Element B(1,2) takes r(1,2) and c(1,2) as the row and column subscripts. And so on...
I can solve this problem using a nested for loop, but the actual application I am working on has very large matrices and this method leads to a significant bottleneck. Are there better (read: quicker) alternatives?
The for loop for reference:
B = NaN(size(r,1),size(c,2));
for row=1:size(r,1)
for col=1:size(c,2)
B(row,col) = A(r(row,col),c(row,col));
end
end