Hi juli jul,
here is a short method to find a small matrix inside another larger matrix
(assuming they are square):
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
int[,] threeByThree = new int[,]
{{1,2,3},
{4,5,6},
{7,8,9}};
int smallMatrixDimension = 3;
int[,] sixBySix = new int[,]
{ {0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,1,2,3},
{0,0,0,4,5,6},
{0,0,0,7,8,9}
};
int largeMatrixDimension = 6;
//ASSUMPTIONS MADE:
//1. largeMatrixDimension is always >= smallMatrixDimension
//2. Both matrices are square
//place the small matrix ontop of the large matrix
//in all possible positions
for (int i = 0; i <= largeMatrixDimension -
smallMatrixDimension; i++)
{
for (int j = 0; j <= largeMatrixDimension -
smallMatrixDimension; j++)
{
//for each position that the small matrix is imposed
//ontop of the large matrix, check to see if the
//items in both matrice match item for item
if (CheckForMatch(threeByThree, sixBySix, i, j))
{
Console.WriteLine(
String.Format("small matrix found at: {0},{1}",
i, j));
}
}
}
}
private static bool CheckForMatch(int[,] matrixToFind, int[,]
matrixToSearch, int xOffset, int yOffset)
{
//assume both matrices are square
for (int i = 0; i <= matrixToFind.GetUpperBound(0); i++)
{
for (int j = 0; j <= matrixToFind.GetUpperBound(0); j++)
{
if (matrixToFind[i, j] != matrixToSearch[i + xOffset, j
+ yOffset])
{
return false;
}
}
}
return true;
}
}
}
Hope that helps
Mark R Dawson
http://www.markdawson.org
"juli jul" wrote:
Hello,
Could someone plz tell me how to implement(!) finding the number
occurrences small matrix inside a bigger one.
(They are static - with fixed size : for example 3*3 inside 6*6)
Thank you very much!
*** Sent via Developersdex http://www.developersdex.com ***