Evaluate all possible combinations of subarrays in an array

2 views (last 30 days)
Hi everyone,
I have an algorithm which is not exactly fast, due to 4 for loops, and I am wondering if the same result can be achieved much faster by using the power of Matlab.
I want to evaluate all possible subarray sizes, each at all possible positions in the main array. The output result is the position and size of a single subarray, which gives the maximum value (critMax) based on the "merit function" (in the example below, the sum over the subarray components, divided by the size)
nx = 100;
ny = 50;
array = rand(nx,ny); %some data array, random numbers are just an example
crit = [0 0 0 0];
critMax = 0;
for Dx = 1:nx %defines width x
for Dy = 1:ny %defines width y
for x0 = 1:nx-Dx %defines start index x
for y0 = 1:ny-Dy %defines start index y
x1 = x0 + Dx - 1; %defines end index x
y1 = y0 + Dy - 1; %defines end index y
subarray = array(x0:x1,y0:y1); %takes the subarray
total = sum(subarray(:)); %sum over whole subarray
res = total/(Dx+Dy); %apply some function, this is just an example, result is a 1x1 double
if res > critMax %compare whether the value in the current subarray is the "best" so far
crit = [x0 y0 x1 y1];
critMax = res;
end
end
end
end
end
crit
critMax
I hope the problem became clear, otherwise I will add more infos.
Thanks a lot in advance.
PS: Matlab R2018a, no additional toolboxes available.

Answers (0)

Categories

Find more on Resizing and Reshaping Matrices 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!