MATLAB Answers

Find sorrunding elements and element from an array

2 views (last 30 days)
Jogger
Jogger on 26 Oct 2020
Commented: Jogger on 28 Jan 2021
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 Comments

Jogger
Jogger on 8 Nov 2020
Thanks a lot adam for your efforts and help !

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 Oct 2020
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 Comments

Show 8 older comments
Image Analyst
Image Analyst on 29 Oct 2020
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
Jogger
Jogger on 8 Nov 2020
Thank you! I'II update further with my comments on your queries with additional information. For the time being this is sufficient

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!