Functions which Return Indices of a Region

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

can you explain with an example?
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

And another way:
halfscale = floor(S / 2);
D = bsxfun(@plus, (-halfscale:halfscale)', (-halfscale:halfscale)*size(X, 1) + Y)
This needs cropping if Y is near the edges, though.
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.
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)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 8 Jul 2015

Commented:

on 8 Jul 2015

Community Treasure Hunt

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

Start Hunting!