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 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 8 Juil 2015
can you explain with an example?
Pyroholic
Pyroholic le 8 Juil 2015
Hello, Azzi. Did you see the illustration I just added? If so, do you still want an example?

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 8 Juil 2015
Modifié(e) : Guillaume le 8 Juil 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 commentaires

Guillaume
Guillaume le 8 Juil 2015
Modifié(e) : Guillaume le 8 Juil 2015
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.
Pyroholic
Pyroholic le 8 Juil 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 le 8 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by