Functions which Return Indices of a Region
1 view (last 30 days)
Show older comments
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
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!