175 views (last 30 days)

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

Image Analyst
on 26 Oct 2020 at 17:25

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

Image Analyst
on 29 Oct 2020 at 13:30

You said "Also please keep "bwareaopen" function c code translation, coder support is needed for c/c++ code generation, otherwise it is pretty difficult to implement on a 32 bit processor." so I was assuming that you needed the whole program to be converted to C code with the Coder Toolbox because you were going to embed this algorithm in a custom chip on some device. If you don't need Coder support and are not going to generate C code, then I don't know why you said that. Please elaboarate. Also, I don't know that Coder can generate code for 32 bit processors. I know MATLAB stopped supporting 32 bit processors with version 2016b.

If you want

For block #1, indexes = 7 8 9 6 5

For block #2, indexes = 41 42 43 40 39

For block #3, indexes = 75 76 77 74 73

then you can just tack on the first two indexes to the end, like 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]

windowWidth = 5;

halfWindowWidth = floor(windowWidth/2);

yMax = movmax(y, windowWidth) > 0; % A logical vector.

yMax = bwareaopen(yMax, windowWidth); % Keep runs of 5 or longer ONLY.

props = regionprops(yMax, 'PixelIdxList') % Requires the Image Processing Toolbox.

for k = 1 : length(props)

fprintf('\nFor block #%d, indexes = ', k);

theseIndexes = props(k).PixelIdxList';

blockIndexes{k} = [theseIndexes(end-halfWindowWidth : end), fliplr(theseIndexes(1:halfWindowWidth))];

fprintf('%d ', blockIndexes{k});

end

fprintf('\nDone running %s.m ...\n', mfilename);

It prints out:

For block #1, indexes = 7 8 9 6 5

For block #2, indexes = 41 42 43 40 39

For block #3, indexes = 75 76 77 74 73

Opportunities for recent engineering grads.

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

Start Hunting!
## 24 Comments

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1087848

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1087848

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1087903

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1087903

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1087913

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1087913

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1087973

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1087973

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1088768

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1088768

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1088773

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1088773

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1088993

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1088993

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089213

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089213

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089353

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089353

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089388

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089388

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089403

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089403

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089453

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089453

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089498

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089498

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089688

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089688

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089758

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089758

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089898

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089898

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089953

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089953

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089988

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1089988

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1090268

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1090268

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1090353

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1090353

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1090938

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1090938

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1091578

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1091578

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1118565

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1118565

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1145718

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-an-array#comment_1145718

Sign in to comment.