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

To extract a submatrix of size 10x10 around (25, 25) from a 50x50 matrix in Matlab, you can use matrix indexing.

main.m
% Create a 50x50 matrix as an example
matrix = rand(50);

% Specify the row and column indices for the top-left corner of the submatrix
row_index = 25;
col_index = 25;

% Extract the submatrix using indexing
submatrix = matrix(row_index:row_index+9, col_index:col_index+9);
274 chars
10 lines

Here, we first create a 50x50 matrix matrix as an example. Then, we specify the row and column indices of the top-left corner of the submatrix as row_index and col_index, respectively. We extract the submatrix of size 10x10 using indexing matrix(row_index:row_index+9, col_index:col_index+9). The : operator means to select a range of indices and +9 is used because we want to select 10 elements (from the top-left corner to the bottom-right corner) in each row and column. The result is stored in a variable named submatrix.

gistlibby LogSnag