Find sorrunding elements and element from an array

1 次查看(过去 30 天)
I have an array
y = [
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0]
where Index 7,41,75 are the locations where 1 is found .
My requirement is
  • create a block around true(1) with a size of 5
  • get the indices like 5,6,7,8,9 and data 0 0 1 0 0
  25 个评论

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2020-10-26
Try this:
fprintf('Beginning to run %s.m ...\n', mfilename);
y = [
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0, ...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0, ...
0 0 0 0 0 0 0 0 0]
yMax = movmax(y, 5)
props = regionprops(yMax > 0, 'PixelIdxList') % Requires the Image Processing Toolbox.
for k = 1 : length(props)
fprintf('\nFor block #%d, indexes = ', k);
indexes{k} = props(k).PixelIdxList;
fprintf('%d ', indexes{k});
end
fprintf('\nDone running %s.m ...\n', mfilename);
You'll see:
For block #1, indexes = 5 6 7 8 9
For block #2, indexes = 39 40 41 42 43
For block #3, indexes = 73 74 75 76 77
  11 个评论
Life is Wonderful
Life is Wonderful 2020-11-8
Thank you! I'II update further with my comments on your queries with additional information. For the time being this is sufficient

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by