471,327 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 software developers and data experts.

Looking for matrix inside a matrix


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 ***
Jan 15 '06 #1
1 1589
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 ***

Jan 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by JustSomeGuy | last post: by
4 posts views Thread by Yudan Yi | last post: by
8 posts views Thread by beginner10 | last post: by
5 posts views Thread by Anolethron | last post: by
6 posts views Thread by lovecreatesbea... | last post: by
14 posts views Thread by Thomas Bauer | last post: by
1 post views Thread by tiddwaylll | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.