Wednesday, January 23, 2013

Finding a vector within a matrix

Imagine, I have a matrix called RGB (list of some colors) like the following 

RGB=[1 2 3; 4 5 6; 7 8 9; 10 11 12; 0 0 0; 1 2 5]

RGB =

     1     2     3
     4     5     6
     7     8     9
    10    11    12
     0     0     0
     1     2     5

I would like to find whether vector [ 1 2 5] is available in RGB or not?

The fastest way is to do some logical operation.

loc =  (RGB(:,1)==1 & RGB(:,2)==2 & RGB(:,3)==5 )

loc =

     0
     0
     0
     0
     0
     1

So now we know where is the vector [1 2 5] is located. We can find its index or even how many times it occurs by manipulating above loc vector

No comments:

Post a Comment

Please ask if anything is not clear enough..........