MATLAB: For loop, exceeded dimension error.

for loopMATLABrow deleting

Hi, this is most certainly an easy fix on which I seem to got myself stuck.
The loop should compare elements of an array and if it finds that they are equal it shall erase that row, next it shall recompute the size of the array and redo the comparison for the next element:
for ii=1:N
if (ii>1) && (ii<N)
i3=ii+1;
for jj=i3:N
if DD(ii,1)==DD(jj,1)
DD(jj,:)=[];
[N,M]=size(DD);
end
end
end
end
I get the following error:
??? Index exceeds matrix dimensions.
Error in ==> VB_CompDin2Darray at 29
if DD(ii,1)==DD(jj,1)
A pointer would be great. Cheers.

Best Answer

Simply run the loop backwards so row 4 doesn't become row 3 when row 3 is deleted.
e.g:
for jj = N:-1:i3
%delete stuff
end
Now of course you're still best off (for speed) building an index vector and doing the whole delete once at the end.
idx = false(N-i3,1);
for jj = i3:N
if stuff
idx(jj) = true;
end
Mat(idx,:) = [];
end