extract submatrix around (25,25) from a (50,50) matrix in matlab

To extract a submatrix around the center of a (50,50) matrix, you can use indexing in Matlab. Here's an example code block that demonstrates the process:

main.m
% Define the size of the matrix
matrix_size = 50;

% Create a sample matrix
matrix = magic(matrix_size);

% Define the center point of the submatrix
center_point = [25, 25];

% Define the radius of the submatrix you want to extract
submatrix_radius = 5;

% Calculate the row and column ranges for the submatrix
row_range = center_point(1)-submatrix_radius:center_point(1)+submatrix_radius;
col_range = center_point(2)-submatrix_radius:center_point(2)+submatrix_radius;

% Extract the submatrix using the row and column ranges
submatrix = matrix(row_range, col_range);
568 chars
19 lines

In this code, we first define the size of the matrix as 50x50. Then, we create an example matrix using the magic() function.

Next, we define the center point of the submatrix as [25,25], and the radius of the submatrix as 5. We then calculate the row and column ranges for the submatrix by subtracting and adding the submatrix radius to the center point.

Finally, we extract the submatrix using the row and column ranges. The resulting submatrix will be a 11x11 matrix, centered around point (25,25) in the original matrix.

gistlibby LogSnag