# [R] Find pattern in matrix

csrabak crabak at acm.org
Fri Jul 22 15:33:41 CEST 2011

```This kind of problem can be addressed with branch and cut or similar
Operations Research methods.

Get a look at the CRAN task view "Optimization and Mathematical
Programming" specially the packages for Integer Programming and Mixed
Integer Programming.

Depending on the demands you have in your project, you may want to
consult with a local OR expert.

Em 21/7/2011 19:12, Q escreveu:
> Hello all,
>
> I'm trying to find all combinations of 4 numbers that satisfy 4 criteria,
> inside of a matrix (62 x 25).  I've found a way to do this using for loops,
> but it is extremely slow because it involves checking every possible
> combination of numbers (567300) to see if the criteria are satisfied.  Do
> you think there is a faster method of doing this?  Or some functions that
> might help me out?
>
> The matrix is of species abundances, with each row representing a species
> (A,B,C...) and each column representing a site (1,2,3...).  The numbers in
> the matrix tell the abundance of the species at that site:
>
>
>
>>      1  2  3...
>> A  0  5  6...
>> B  7  8  2...
>> C  4  1  3...
>> ...
>>
>
> I'm trying to find combinations of species/sites such that
> A1>B1,A1>A2,B2>B1,B2>A2
>
> I have used expand.grid to get all combinations of species pairs and site
> pairs, and then checked to see if the inequalities are satisfied.  However,
> as I said, it takes forever.
>
> I tried to write a function that would take vectors and trick R into
> searching for my pattern:
>
>
>
>> count<- function(i,j,k,l){
>> length(matrix[matrix[i,k]>  matrix[i,l]&&
>> matrix[i,k]>  matrix[j,k]&&
>> matrix[j,l]>  matrix[j,k]&&
>> matrix[j,l]>  matrix[i,l]])
>> }
>>
>
> That didn't work.  So I wonder if anyone has any ideas of another way to
> proceed or functions I could look up that would head me in the right
> direction.  Something fully fleshed out isn't necessary.
>
> Thanks!
>
> Q
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Find-pattern-in-matrix-tp3685278p3685278.html
> Sent from the R help mailing list archive at Nabble.com.
>

```