Lorenzo Pistone
Lorenzo Pistone

Reputation: 5188

getting two specific elements of a matrix

Is there a shorthand to get specific elements out of a matrix? Say, I have a matrix m, and I want to get element (a, b) and (c, d). Can I do this in a single expression?

Upvotes: 0

Views: 65

Answers (2)

bla
bla

Reputation: 26069

Another options (and in my view a simpler one) is just:

 val=[m(a,b) m(c,d)];

Upvotes: 1

carandraug
carandraug

Reputation: 13081

Yes. You can use sub2ind to convert a list of subscripts to index. This works for any number of dimensions. From your example, if you want to get matrix(a, b) and matrix(c, d), you can instead do:

values = matrix(sub2ind (size (matrix), [a c], [b d]))

Upvotes: 0

Related Questions