running time
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 19 Okt. 2011
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.
Weitere Antworten (1)
Daniel Shub
am 19 Okt. 2011
Since mat(i, 2) will always equal mat(j, 2) when i is equal to j and you will always set mat(i, 2) to zero unless it is already equal to zero. I think at the end you have effectively done
mat(:, 2) = 0;
the mat2(i, 2) part is basically
mat2(:, 2) = 1:p;
mat2(mat(:, 2) == 0, 2) == 0;
This leaves the count part. You are looking for how many matches of mat(i,2) there are in mat(:, 2). I think you can do this with a hist function.
n = hist(mat(:, 2), unique(mat(:, 2));
The final step is putting the n's into x. I think there are probably quick ways of doing this.
2 Kommentare
Daniel Shub
am 19 Okt. 2011
The inability to format code in a comment means I will not answer you here. I apologize since this isn't your fault. If Walter has answered your original question, then accept his question and ask a new one with your new code. If he hasn't, but has helped to refine your question, edit your question where you can add code markup. On a side note, trim your code as much as possible. I am not going to be able to get past the first line since I do not have ws.txt (nor do I want it).
Siehe auch
Kategorien
Mehr zu Point Cloud Processing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!