MATLAB Answers

Find sorrunding elements and element from an array

64 views (last 30 days)
Matlab
Matlab on 26 Oct 2020
Commented: Matlab on 19 Nov 2020 at 10:15
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

  24 Comments

Matlab
Matlab on 28 Oct 2020
You have A_idx = [5 6 7 8 9] but expectation is A'_idx = [7 8 9 6 5]
You have A_idx = [0 0 1 0 1] but expectation is A'_bits = [1 0 1 0 0]
Rules:
  • The first / every row must start with error bit location followed by rest of bit. i.e. leading coefficeint of a the polynomial must be 1
  • Tail bits of a polynomial can be any 0/1 in a block size( we get in the array). Bitreversal is done for 5,6 to 6,5
  • Once a error bit is processed in a block , we don't need to processs the bit again. Block window should slide further in search of next index / error bit to make a polynomial of fixed size
  • Minimum polynomial size i.e. block of 5 is reasonable .
  • polynomial size should not change during the execution.
Thank you!
Matlab
Matlab on 8 Nov 2020 at 16:34
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

  10 Comments

Show 7 older comments
Image Analyst
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
Matlab
Matlab on 8 Nov 2020 at 16:33
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!

Translated by