diag(X(:,c))
should do the trick
Explanation:
A (slightly more complicated) example will help understand.
>>X = [1 2; 3 4; 5 6; 7 8]
X =
1 2
3 4
5 6
7 8
>> c = [1 1 2 1];
>> R = X(:,c)
R =
1 1 2 1
3 3 4 3
5 5 6 5
7 7 8 7
So what's going on here? For each element in vector c
, you're picking one of the columns from the original matrix X
: For the first column of R
, use the first column of X
. For the second column of R
, use the first column of X
(again). For the third column of R
, use the second column of X
... and so on.
The effect of this is that the element you're interested in (defined in c
) is located along the diagonal of the matrix R
. Get just the diagonal using diag
:
>>diag(R)
ans =
1
3
6
7