MATLAB: Decrease calculation accuracy to speed up running time

decrease calculation accuracydelete entry in arraymemoryperformanceslow

hi, I want to write script but I have huge input and I know if I run this script I have to wait long time to get output.
so I want from MATLAB to calculate all data with medium accuracy to speed up my program.
for example MATLAB calculate pi to 32 digits and I want to calculate it for example only to 10 digits to speed up in calculation.
look! I dont want to display a number with low digits by "format" command.
so , what should I do to decrease calculation accuracy ???

Best Answer

The reason your algorithm is taking so much time is because of the line
COPT(u+1,:)=[];
What happens when you delete an entry to an array is MATLAB has to create an entirely new array in memory. Since you are deleting tens of thousands of entries, you are creating tens of thousands of arrays. The improved way to perform this loop is to simply mark an entry for deletion within the loop, and then delete all the invalid rows in one fell swoop outside the loop.
There may even be a way to vectorize this operation, but I think the following loop method is sufficient and, importantly, instructive. Replace the while loop at the bottom with the following
u = 1;
tbd = false(length(COPT),1); % "to be deleted"
while u <= size(COPT,1)-1
s = 1; % this is the "next" spot
while u+s <= size(COPT,1) && COPT(u,1) == COPT(u+s,1)
COPT(u,2) = COPT(u,2) + COPT(u+s,2);
tbd(u + s) = true; % mark this spot for deletion later
% increment s, and check again until it no longer equals
s = s + 1;
end
u = u + s; % increment u
end
% delete invalid entries in COPT
COPT(tbd,:) = [];
Now even with the large scale input, this takes 0.6 seconds to run on my machine, whereas before it was 10 minutes before I stopped execution (and it was only on iteration 476 out of 451817).
I also ran it on a small scale and ensured that the two algorithms produced the same result.