Q:

matlab matrix get values in different columns

Let my matrix be:
        A = 
        [1,2,3;
        4,5,6;
        7,8,9];

I have to extract the values in (1,2), (2,3), (3,1).
I tried the following:
        c = [2,3,1]';

        A(:, c)

but it gives a 3x3 matrix instead of 3x1.
How can I do it efficiently in matlab?

A:

The best way to extract arbitrary values from a matrix is to use linear indices.
You can use sub2ind
ind=sub2ind(size(A),[1 2 3], [2 3 1]);
val=A(ind);

