0

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?

horchler
  • 18,384
  • 4
  • 37
  • 73
user570593
  • 3,420
  • 12
  • 56
  • 91

1 Answers1

2

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);
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120