Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a vector within a vector

Tags:

matlab

I have an (7,6) logical array, such as the following:

validY2_A =

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

I want to create a (1,6) logical vector 'contig' that shows whether the above matrix has 3 contiguous ones in each column. For example, the outcome of this would be:

contig =

[1, 1, 1, 0, 0 ,1];

I've tried strfind but there are two issues with this, the first being that it is an array of columns (transposing either didn't help at all. or would require lots of extra code to use temporarily. The second issue is that because it is a logical array, if I change it to a string, the values all become true or false, and trying to get that to work has also been fruitless. Is there a method to search a column vector to find if another, specific column vector exists anywhere within it? I want to search each column for [1; 1; 1];

Thanks

like image 829
TheMcCleaver Avatar asked Apr 02 '13 14:04

TheMcCleaver


2 Answers

How about

t = imfilter( validY2_A, ones(3,1) );
contig = any( t >= 3, 1 );

Alternatively (as suggested by @Dan):

t = conv2( validY2_A, ones(3,1), 'same');
contig = any( t >= 3, 1 );

As suggested by @GeorgeAprilis, one might need to convert the logical matrix validY2_A to double first:

validY2_A = double( validY2_A );
like image 197
Shai Avatar answered Oct 12 '22 07:10

Shai


Here is a way that should be easy to understand:

idx1=1:end-2
idx2=2:end-1
idx3=3:end

Basically these indexes shift your matrix three times.

Now you just apply them:

any(validY2_A(idx1,:) & validY2_A(idx2,:) & validY2_A(idx3,:))

This is not too hard to generalize using a loop and shift function for example.

like image 27
Dennis Jaheruddin Avatar answered Oct 12 '22 08:10

Dennis Jaheruddin