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

7 Ansichten (letzte 30 Tage)
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.

Antworten (1)

Image Analyst
Image Analyst am 4 Okt. 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
mask = 2×5 logical array
0 1 1 1 0 1 1 1 0 0
maskedValues = A(mask) % Get a 1-D list of values in the irregularly-shaped mask.
maskedValues = 6×1
6 2 7 3 8 4
maxValue = max(maskedValues)
maxValue = 8
  4 Kommentare
MAM09
MAM09 am 4 Okt. 2022
Bearbeitet: MAM09 am 5 Okt. 2022
Okay, thanks. The number of submatrices is about several hundred thousands. So, it is taking about 30 seconds with a loop.
Image Analyst
Image Analyst am 5 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by