Functions which Return Indices of a Region

1 view (last 30 days)
Is there a function in MATLAB that takes as input: a matrix, 'X', an index, 'Y', and a scale 'S', and returns a matrix of indices, 'D', corresponding to a square in 'X', with center 'Y', and length equivalent to twice the scale, 'S'?
EDIT: Added the figure for illustration
  2 Comments
Pyroholic
Pyroholic on 8 Jul 2015
Hello, Azzi. Did you see the illustration I just added? If so, do you still want an example?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 8 Jul 2015
Edited: Guillaume on 8 Jul 2015
There are many way to do this. One of them:
allindices = reshape(1:numel(X), size(X));
[centrerow, centrecol] = ind2sub(size(X), Y);
halfscale = floor(S / 2);
D = allindices(max(1, centrerow - halfscale) : min(size(X, 1), centrerow + halfscale), ...
max(1, centrecol - halfscale) : min(size(X, 2), centrecol + halfscale))
  3 Comments
Pyroholic
Pyroholic on 8 Jul 2015
Thank you, Guilaume for helping me out. I highly recommend the answer in the first comment. The one in the second comment gave me wrong results. Thanks again.
Guillaume
Guillaume on 8 Jul 2015
There was an extra bracket in the second answer which I have now removed. Both answers should give the exact same result for Y that's not near the edges.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!