Given a matrix of arbitrary dimensions where under a certain antidiagonal all values are unknown, NaN, I want to extract submatrices without NaN values which are as large as possible such that as many elements of the original matrix are contained in the submatrices. Each submatrix must be at least R by R, where R can be specified by the user. The submatrices may overlap each other. I'm able to find the anti-diagonal under which all elements are NaN, but I don't really know how to divide the non-NaN part of the matrix in submatrices which try to cover as much of the known values as possible.
The following image illustrates how the numeric part of the matrix might be partly divided in submatrices for R = 3:
Possible submatrix division
I have the following code:
mask = ~isnan(A);
nrKnownElsRow = sum(mask,2);
dims = [[1:length(nrKnownElsRow)]' nrKnownElsRow];
dims(dims(:,2)<R,:) = [];
dims(dims(:,1)<R,:) = [];
idx = find(dims(:,2) == max(dims(:,2)));
dims(idx(1:end-1),:) = [];
dims = [dims(:,1) zeros(length(dims),1) dims(:,2)];
for k = 1:length(dims)-1
if dims(k,3) - dims(k+1,3) >= R
dims(k,2) = dims(k+1,3) + 1;
else
dims(k,2) = dims(k,3) - R + 1;
end
end
dims(end,2) = 1;
The dims variable contains in each row for the corresponding submatrix the row up to which the subtensor goes, then the outer columns that make up the submatrix. This uses the most information possible in the matrix, but the submatrices are not as large as possible.
The following solution is not perfect but it may help you get started.
This script catches all submatrixes with 1 or both sides >
r(the threshold you defined as R).generating
Afind NaNs coordinates
Now
L2xandL2ycontain all single-coordinate combinationscombining
L2xandL2yinto single variableL3remove any submatrix containing NaNs
calculating all submatrix combinations, pairs, no repetition
attaching marker to each
cnL3line telling whether a1(a2), or a2(a1), or a1==a2.cnL3format :[ ... A51_contains_A52 A52_contains_A51]who contains who
pairs with contained submatrixes
removing contained submatrixes from submatrix list
L3X: cell containing all complying submatricesreading 1 element of cell
Xhow to do a cell all-out
Just typing
XMATLAB returns all sizes of elements inside cellXnow the threshold
ryou requiredThis works for the few cases I checked.
Note I generate test matrixes with no NaNs along perimeter.
Stijn, if this solution helps would you please be so kind to consider green-ticking it as valid answer?
thanks for reading