From: Biljana on
Hi,

I have a function in Matlab that accepts a matrix, and creates all monotonic submatrices that are monotonic, by deleting rows from the original matrix. The new matrices are saved in a tree like nested cell array. I need a way to automatically read the leaves of the tree so that I get to the monotonic matricies. Or to read the leaves of the tree and save them in a new structure/cell array that is easy to read with a loop. The code is following:

function armat=createmonotonic(k)
M=[];
M1=[];
M=sortall(k); % sorts the whole matrix starting from last column
[r c]=size(M);

D=diff(M);

dom=zeros(r,1);

for ii=1:r-1
for jj=1:c
if D(ii,jj) < 0
dom(ii+1)=1;
break;
end
end
end

jj=1;

if any(dom) % if there are no one in dom, it means that the matrix is monotonic so return it

for ii=1:r
if (dom(ii)==1)
M1=[M(1:ii-1,:); M(ii+1:end,:)];
armat{jj}=createmonotonic(M1);
jj=jj+1;
if (ii>2)
M2=[M(1:ii-2,:); M(ii:end,:)];
armat{jj}=createmonotonic(M2);
jj=jj+1;
end
end
end

else
armat=M;
end

As an example try the following matrix
B = [ 1 1 1; 1 1 2; 1 2 2; 1 2 3; 2 2 3; 2 3 3; 2 3 4; 3 3 4; 3 4 4; 4 4 4; 1 2 1; 2 1 2];
and then
armat=createmonotonic(B);

I tried to read with recursion but i constantly get the error: Maximum recursion limit of 500
reached.

I really hope someone can help.
 | 
Pages: 1
Prev: Function Tip
Next: Exporting time to Excel