Find sorrunding elements and element from an array

조회 수: 1 (최근 30일)
Life is Wonderful
Life is Wonderful 2020년 10월 26일
댓글: Life is Wonderful 2021년 1월 28일
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