user3495562
user3495562

Reputation: 335

Choose multiple values from multiple rows and columns from matrix in matlab

Say I have a matrix

A = [1, 2, 3; 4 5 6; 7 8 9]

If I want to choose say (1,2), (2,3)

I could not say A(1:2,2:3) otherwise it will return a 2*2 matrix, what should I do it for only one time...

Upvotes: 2

Views: 588

Answers (1)

Divakar
Divakar

Reputation: 221574

Use sub2ind to linearly index (to avoid creating a matrix) using the given row and column numbers -

ind = [1,2;2,3]
A(sub2ind(size(A),ind(:,1),ind(:,2)))

Upvotes: 4

Related Questions