MATLAB: Running time

runningtime

hi,
the running time of the following code is very long:
%%%%%%%%%%%%%%
for i=1:p
count=0;
for j=1:p
if mat(i,2)~=0
if mat(i,2)==mat(j,2)
mat(i,2)=0;
mat2(i,2)=i;
count=count+1;
x(i)=count;
end
end
end
end
%%%%%%%%%%%%%
where p=211000
are there anyway to make it faster?
thanks in advance

Best Answer

You overwrite x(i) with count each time the condition matches within "j", but you do not set x(i) at all if the condition never matches, so unless it is important that x not be any longer than the last match found, you can move the assignment down to below the end of the "j" for loop and do it unconditionally.
There may be other changes that will be easier to see once you have done this rewrite.
Related Question