0

I am trying to index a matrix (image read with imread()) with vectors for row and column numbers.

map is the image read with imread(), and rows and columns are the 1-dimensional index vectors.

But when I do map(rows, columns) it returns a 2-dimensional matrix, where I presume it is using every single permutation of rows and columns to index map.

How do I index map with each corresponding element at (rows,columns)?

I believe the answer I am looking for is equivalent to diag( map(rows,columns ) but it's a bit inefficient I think.

Shreyas
  • 667
  • 2
  • 7
  • 20

1 Answers1

0

If I got you right what you want is to extract a vector of pixels where each pixel is a combination of row and column index.

If I got you right, the way to do it is using sub2ind.
You can do something like that (Assuming Image is Grayscale Image):

vPixelIdx = sub2ind(size(mInputImage), vRowsIdx, vColIdx);
vPixels = mInputImage(vPixelIdx);

I didn't test it as I don't have access to MATLAB now, but it should work.

Royi
  • 4,640
  • 6
  • 46
  • 64