4 views (last 30 days)

Hi everyone,

as the title says I'm looking to find an exact sequence of values in a matrix. I have a matrix that I'm turning into a logical array as below. Now I want to be able to say if the consecutive ones in the column are less than X (as in e - see below) they should also be turned into zeroes as well.

I tried to work with find(contain()) but that did nothing. Also ismember() did not turn out the results I was looking for.

Turning matrix into binary representation

for k = 1:1:size(p,1)

for l = 1:1:(size(p,2))

if p(k,l) > z

p(k,l) = 1;

else

p(k,l) = 0;

end

end

end

Logical array

0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

0 1 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 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0

0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0

0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0

0 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0

0 0 1 0 1 1 1 0 0 0 0 1 0 0 0 0

0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0

0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0

0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0

0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0

0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0

0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0

0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1

Size of ones I would like to be able to filter

e = ones(x,1)

Thank you for your help everyone! It's greatly appreciated.

Cheers,

Peter

Luna
on 2 Dec 2019

Edited: Luna
on 2 Dec 2019

Try Loren's findpattern2. Here is link:

create vector x numbers of ones and run findpattern2.

e = ones(1,x);

for i = 1:size(LogicalArray,2)

indices = findpattern2(LogicalArray(:,i),e);

if ~isempty(indices) % if it finds x elements of this pattern make them zero.

for k=1:numel(indices)

LogicalArray(indices(k):indices(k)+x,i) = false; % from indice to pattern length will be zero

end

end

end

Image Analyst
on 3 Dec 2019

If you want to use functions already built in to the toolbox, you can use bwareafilt:

binaryImage = logical([...

0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

0 1 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 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0

0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0

0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0

0 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0

0 0 1 0 1 1 1 0 0 0 0 1 0 0 0 0

0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0

0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0

0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0

0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0

0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0

0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0

0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1])

x = 4; % Whatever....

% Get rid of stretches of 1's in each row that are less than x long.

for row = 1 : size(binaryImage, 1)

binaryImage(row, :) = bwareafilt(binaryImage(row, :), [x, inf]);

end

The result is:

binaryImage =

17×16 logical array

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 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 1 1 1 0 0 0 0 0 0 0 0 0

0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0

0 0 0 0 1 1 1 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 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Opportunities for recent engineering grads.

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_772652

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_772652

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_772654

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_772654

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_772658

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_772658

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_773460

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_773460

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_773494

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_773494

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_773720

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/493882-how-to-find-an-exact-sequence-of-values#comment_773720

Sign in to comment.