Problems generalizing a number sequence

  • Thread starter Thread starter roldy
  • Start date Start date
  • Tags Tags
    Sequence
AI Thread Summary
The discussion revolves around generalizing the location of the center point of a 3x3 raster segment within a larger logical matrix. The original matrix's dimensions impact the number of possible raster segments, and the user initially attempted to use logical statements to determine the row and column of the center point based on segment numbers. However, this method proved cumbersome as matrix sizes increased, leading to a need for a more efficient mathematical relation or table. Ultimately, the user devised a solution using MATLAB that involves creating arrays for row and column numbers based on the dimensions of the original matrix and the segment matrix. This approach effectively computes the center point's location for any size original matrix.
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];
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top