Problems generalizing a number sequence

  • Thread starter Thread starter roldy
  • Start date Start date
  • Tags Tags
    Sequence
Click For Summary
SUMMARY

This discussion focuses on generalizing the computation of the center point location of 3x3 raster segments within a larger logical matrix. The user initially attempted to use logical statements in MATLAB to determine the row and column indices based on segment numbers but found this approach inefficient for larger matrices. The solution involves calculating the row and column indices dynamically using the dimensions of the original matrix and the segment matrix, ultimately leading to a more scalable method. The final MATLAB code provided efficiently generates the center point locations for any size original matrix.

PREREQUISITES
  • Understanding of matrix operations in MATLAB
  • Familiarity with logical indexing and array manipulation
  • Knowledge of raster graphics concepts
  • Basic mathematical skills for deriving equations
NEXT STEPS
  • Explore MATLAB's array manipulation functions for efficient data handling
  • Learn about raster graphics and their applications in image processing
  • Investigate mathematical modeling techniques for matrix transformations
  • Study optimization strategies for large-scale matrix operations in MATLAB
USEFUL FOR

Mathematicians, data scientists, and software developers working with image processing or matrix computations who need to optimize raster segment analysis in MATLAB.

roldy
Messages
206
Reaction score
2
I'm having problems generalizing a number sequence. It involves a couple parameters that I will attempt to explain and show by example.

Basically I have a program that takes 3x3 raster segments of a much bigger matrix. The matrix is a logical matrix. What I do is take the a 3x3 raster segment and compare it to a library of 3x3 patterns. I check to see if this particular segment matches any of the 3x3 patterns in the library. This happens until the rastering process is over.

If a match is made in the library, I need to place a marker in the original matrix. The marker is always in the center of the raster segment (2,2) and at different locations in the original matrix depending on what number raster it is.

I need to come up with a generalized equation for any size original matrix such that I can compute the row and column location in the original matrix of the center point in the raster segment. In other words transform the center point (2,2) in the raster segment to it's corresponding point in the original matrix.

Note: The logical matrix is read as a column in matrix M such that every column represents a raster segment.

For example in the original matrix [O]:

Code:
O = [0 1 0 1 1;
     1 0 1 1 1; 
     1 0 1 0 1;
     1 1 0 0 1;
     1 1 1 0 1];

Raster segment matrix
Code:
M = [0 1 0 1 0 1 1 0 1;
     1 0 1 0 1 1 0 1 0;
     0 1 1 1 1 1 1 0 1;
     1 0 1 1 0 1 1 1 0;
     0 1 1 0 1 0 1 0 0;
     1 1 1 1 0 1 0 0 1;
     1 0 1 1 1 0 1 1 1;
     0 1 0 1 0 0 1 1 0;
     1 0 1 0 0 1 1 0 1];

Each column represents a raster segment read row wise (every pair of 3 in a column of M = a row in the raster segment).

So for the first segment (column 1),
Code:
Seg1 = [0 1 0;
        1 0 1;
        1 0 1];

For this example the locations of the center (2,2) for the raster segments 1 - 9 are as follows:

Raster Segment# = (row,col)

1 = (2,2)
2 = (2,3)
3 = (2,4)
4 = (3,2)
5 = (3,3)
6 = (3,4)
7 = (4,2)
8 = (4,3)
9 = (4,4)

At first I was thinking about using a logical statement like so in where I compare the segment number (Sn) to a listing. any() checks to see if any of the equivalencies are true; returns a 1 for true, 0 for false.

Code:
row = 2*(any(Sn == [1 2 3])) + 3*(any(Sn == [4 5 6])) + 4*(any(Sn == [7 8 9]));

col = 2*(any(Sn == [1 4 7])) + 3*(any(Sn == [2 5 8])) + 4*(any(Sn == [3 6 9]));

So for example, if I am on Sn = 2, the calculation for row and column would be as follows

row = 2*(1) + 3*(0) + 4*(0) = 2
col = 2*(0) + 3*(1) + 4*(0) = 3

location of (2,2) in original matrix is (2,3)

Here's the problem with this approach. For any size [O], I will have more array checks. For instance, if I were to have a 6x6 original matrix the number of raster segments would be 16. This is found by the following relation:

# of possible horizontal rasters = row size of [O] - 2
# of possible vertical rasters = column size of [O] - 2

So 4 raster segments are possible for horizontal scanning and 4 for vertical scanning.

The row and column equations would then become:

row = 2*(any(Sn == [1 2 3 4])) + 3*(any(Sn == [5 6 7 8])) + 4*(any(Sn == [9 10 11 12])) + 5*(any(Sn == [13 14 15 16]));

col = 2*(any(Sn == [1 5 9 13])) + 3*(any(Sn == [2 6 10 14])) + 4*(any(Sn == [3 7 11 15])) + 5*(any(Sn == [4 8 12 16]));

For obvious reasons this won't work as nicely as I thought. So now I'm trying to figure out a mathematical relation or perhaps generate a table. I'm using MATLAB to do this and would appreciate any suggestions.
 
Technology news on Phys.org
Update:

I have figured this out if anyone happens to come across this post.
rO = # rows in matrix O
cO = # of columns in matrix O

cM = # of columns in segment matrix M


rownums = repmat([2:rO-1],cM/(rO-2),1);
rownums = rownums(:);

colnums = repmat([2:cO-1]',1,cM/(cO-2));
colnums = colnums(:);

locations = [rownums,colnums];
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K