# about finding maximum of submatrix at specific locations within a bigger matrix

8 views (last 30 days)
MAM09 on 4 Oct 2022
Commented: Image Analyst on 5 Oct 2022
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.

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]
A = 2×5
1 2 3 4 5 6 7 8 9 10
submatrix = A(:, 2:4)
submatrix = 2×3
2 3 4 7 8 9
% Find the max of the submatrix
maxValue = max(submatrix(:))
maxValue = 9
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
0 1 1 1 0 1 1 1 0 0
6 2 7 3 8 4
maxValue = 8
Image Analyst on 5 Oct 2022
Let's just look at the loop alone, ignoring anything done inside it:
tic
for k = 1 : 500000
; % Do nothing but iterate
end
toc
Elapsed time is 0.000283 seconds.
OK so 283 microseconds. So the iteration itself is not the bottleneck. But if your matrices locations are going to be wandering all over the place, I don't see any reasonable alternative to doing it in a loop.

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by