[Matlab] Transform generated table to equation for for loop usage

In summary, the conversation discusses the use of equations instead of a lookup table for finding the location of the center point in a 3x3 raster segment within a larger matrix. The conversation also includes a discussion on optimizing code for efficiency, specifically in regards to nested for loops and memory usage. Ultimately, the solution is to use equations to directly calculate the row and column location in the larger matrix instead of using a table lookup.
  • #1
roldy
237
2
I have posted a question on here before regarding the generation of a number sequence. I followed up that question with an answer. However, as I have developed my code more I need to use an equation instead of a lookup table.

Note: I'm using MATLAB

Given a matrix A of size r x c where r >= 3, c >= 3 I need to map the center point of a 3 x 3 raster segment to a point in A. The raster segments go from left to right, top to bottom. For example, if

Code:
A = [0 1 1 0 1 0 1 0 1;...
      0 0 1 0 0 1 1 0 1;...
      1 0 1 1 0 1 0 1 0;...
      1 1 1 1 1 1 1 1 1];...

The location of (2,2) in each raster segment can be found as follows.

Code:
%raster binary image matrix into 3x3 segments
M = im2col(FP, [3,3],'sliding');

[ri ci] = size(A);
[rM cM] = size(M);

%Generate table of locations in binary image matrix for center point (2,2) of segment matrices
rownums = repmat([2:ri-1],cM/(ri-2),1);
rownums = rownums(:);

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

locations = [rownums,colnums]

I only need matrix M to find what rM and cM are. Instead of using the im2col() function, I can use the equations below. cM is the number of total raster segments

Code:
rM = 9;
cM = (ci-2)*(ri-2);

I do some matrix operations for each raster segment using a for loop

Code:
n = 1;

k = 1; 
for row = 1:ri-2
 
	for col = 1:ci-2
  
           %some operations
                
                if %condition is met
                    %transform center point location in raster segment
                    %to corresponding location in matrix A
                    Iloc(n,:) = locations(k,:);
                    n = n+1; 
                end
            
            else   %if condition is not met, increment segment counter k and continue
                    %in for loop to next segment
                k = k+1;
                continue
            end
        k = k+1;
	end
	
end

I would like to replace this line of code
Code:
Iloc(n,:) = locations(k,:);
with equations for the row and column location in A instead of looking up in the table for these locations. The reason for this is that if I have a matrix A of size 274 x 234, I would have 63104 raster segments. This means that my location table matrix would be 63104 x 2. This takes up a lot of memory with the other stuff that I am doing. I know it's possible to come up with some equation that's dependent on the value of row, col, and k. It seems that every (ci-2) kth value, the row in A increments by one. For every different row value, the column values go from 2:(ci-1).
Question is how do I activate the row increment and column range for every (ci-2) kth value?
 
Physics news on Phys.org
  • #2
My first reaction is that instead of summing over rows and columns you can probably vectorize the operation to save a lot of memory. Nested for loops are really expensive here, so it's worth looking in to.

Also, im2col() puts the 3x3 matrices in the columns of M, so that M(5,:) is the row vector of the (2,2) element in each 3x3 matrix.

Then

m = reshape(M(5,:),272,232)

turns that vector of centers into the same form as it is in A, and is equivalent to A with the edge values shaved off, A(2:end-1,2:end-1).

Each element in this matrix corresponds to the center of one of the 3x3 matrices, and the indices are each padded by 1, i.e. the (1,1) element in m is the (2,2) element in A, and so on.
 
  • #3
kreil, within the nested for loops I submit the raster segment to a trained neural network to see if there is a match (part of the project criteria). The simulation of the neural network goes in place of "some operations". If I return a match, then I proceed to get the location from the table that's the if logic statement. Once I get all the locations, I use a logical mask to color a pixel red at transformed locations. There are two ways that I can go about this. The first method I tried was to just use one for loop that cycles through each raster segment in M. This is originally why I needed the table for the locations. The reason why I'm leaning towards the nested for loop is because I don't have to store anything, such as the huge M matrix or the table locations if I can transform the table calculations to equations. Any thoughts on this?

I've attached the necessary files to run this program. The main program is locateBifurcation. I've included a fingerprint image in case you wanted to test this on a full fingerprint instead of the small test logic matrix I created. The image must first get passed into imread() then into preprocess(I) to convert to a binary matrix.
 

Attachments

  • files.zip
    58 KB · Views: 247
Last edited:
  • #4
I think I figure this out. Simple solution actually, don't know why I didn't think of this before. I just replaced the table look up with the row and col value added by one from the for loops.
Code:
if %condition is met
   %transform center point location in raster segment
   %to corresponding location in matrix A
   Iloc(n,:) = [row+1,col+1];
   %Iloc(n,:) = locations(k,:);
   n = n+1; 
end
 
  • #5


I would suggest approaching this problem by breaking it down into smaller steps and using mathematical equations to represent each step. This will not only help with reducing memory usage but also make the code more efficient and easier to understand.

First, let's consider the overall goal of the code - to transform the center point location in a raster segment to the corresponding location in matrix A. This can be represented by the equation:

A_location = location + (segment_number - 1) * (segment_size - 1)

where A_location is the corresponding location in matrix A, location is the center point location in the raster segment, segment_number is the number of the current segment, and segment_size is the size of the segment (in this case, 3).

Next, let's consider how to determine the segment_number. Since the segments go from left to right, top to bottom, we can use the equation:

segment_number = (col-2) + (row-2) * (ci-2)

where col and row are the current column and row values in the for loop, and ci is the number of columns in matrix A.

Finally, we need to determine the center point location in the raster segment. This can be done using the equations:

row_loc = 2 + mod(segment_number-1, (ci-2))
col_loc = 2 + floor((segment_number-1)/(ci-2))

where row_loc and col_loc represent the row and column location in the raster segment, respectively.

Combining these equations, we can now rewrite the code as:

for row = 1:ri-2
for col = 1:ci-2

%some operations

if %condition is met
%transform center point location in raster segment to corresponding location in matrix A
A_loc = [row col] + (col-2) + (row-2) * (ci-2);
Iloc(n,:) = A_loc;
n = n+1;
else
%if condition is not met, continue to next segment
continue
end

end
end

By using these equations, we have eliminated the need for the location table matrix, reducing memory usage and making the code more efficient. Additionally, the equations are dependent only on the values of row, col, and ci, making the code more adaptable to different matrix sizes.
 

1. How can I transform a generated table in Matlab into an equation for for loop usage?

To transform a generated table in Matlab into an equation for for loop usage, you can use the "table2array" function to convert the table into an array. Then, you can use the array values in a for loop to perform calculations or other operations.

2. Can I use a for loop to perform operations on each row or column of a generated table in Matlab?

Yes, you can use a for loop to iterate through each row or column of a generated table in Matlab. You can use the "size" function to determine the number of rows or columns in the table and then use a for loop to perform operations on each row or column.

3. How do I access specific values in a generated table in Matlab for use in a for loop?

To access specific values in a generated table in Matlab, you can use the "table" function to create a table variable and then use the variable name followed by curly brackets and the row and column indices to access the desired value.

4. Is it possible to transform a generated table in Matlab into an equation for for loop usage without using the "table2array" function?

Yes, it is possible to transform a generated table in Matlab into an equation for for loop usage without using the "table2array" function. You can use the "struct2array" function to convert the table into a structure and then use the structure values in a for loop.

5. Can I use a for loop to modify the values in a generated table in Matlab?

Yes, you can use a for loop to modify the values in a generated table in Matlab. You can use the "table" function to create a table variable and then use the variable name followed by curly brackets and the row and column indices to modify the desired value.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
935
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
4K
Back
Top