i have a 2D matrix of characters or integer or whatever!How do i check if another matrix is a sub-matrix of The 2D one ...Please!! help with simple code that work for any size of the matrices


Thanks in advance to all

Recommended Answers

All 2 Replies

Fun problem. :) Solving it brute force is easy though. Just go through every element in the big matrix and look for a sub matrix starting at that point.

foreach row in bigMat
  foreach col in bigMat
    currRow = row

    for i = 0 to smallMat.Rows
      currCol = col

      for j = 0 to smallMat.Cols
        if bigMat[currRow][currCol++] != smallMat[i][j]
          return false
        endif
      endfor

      currRow++
    endfor
  endforeach
endforeach

return true

Edward offers no warranty for that algorithm. It was written inline and on a lunch break, two things that are known to sap Ed's programming awesomeness. ;)

Thanks Edward i will try to implement that ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.