about finding maximum of submatrix at specific locations within a bigger matrix
4 views (last 30 days)
Hello, I am looking to find the maximum value of a given size submatrix at multiple locations of a bigger matrix without using any loops. For a toy example, A = [1,2,3,4 5;6,7,8, 9,10] is my original matrix and I would like to get the max of the submatrix [2,3,4;7,8,9] which happens to be 8.
Using sepblockfun does not work.
Thank you in advance.
Image Analyst on 4 Oct 2022
That's not a matrix. Matrices need to be rectangular. You can't start the first row in column 2 of A and the second row in the column 1 of A like you did with 2,3,4 in row 1 and 6,7,8 in the second row.
But if you define a submatrix using rows and columns that are valid you can do
A = [1,2,3,4 5;6,7,8, 9,10]
submatrix = A(:, 2:4)
% Find the max of the submatrix
maxValue = max(submatrix(:))
If you really want that jagged, non-rectangular region, you'll have to define a binary mask saying what elements you want included or not included:
mask = logical([0, 1, 1, 1, 0; 1, 1, 1, 0, 0]) % Create non-rectangular mask
maskedValues = A(mask) % Get a 1-D list of values in the irregularly-shaped mask.
maxValue = max(maskedValues)