my code become busy for euclidean distance

i write code for euclidean distance in nested for like this:
for rsize=1:tests
for k=1:ts
dis(k,1)= pdist2( testset(rsize,1:57),tmatrix(k,1:57),'euclidean');
end
end
its run time is very long 1-2 hours why? how can i correct this code?

4 Commenti

You haven't said what values of 'tests' and 'ts' you used for your hours-long run. They must be rather large for the run to last that long.
The question I have is, why do you keep repeating the computation for every combination of values of 'rsize' and 'k'? If you simply do it once for the maximum of each:
dis = pdist2( testset(tests,1:57),tmatrix(ts,1:57),'euclidean');
the array 'dis' would contain all the pair distances that you would get the long way. Doing it for all those combinations is a horrific waste of computation time and computer memory with all that useless repetition, (as you have learned the hard way.)
tests:2301 ts=2300 really i have train set and test set matrix that i have to compute the euclidean distance of every row of test matrix with all rows of train matrix every time and i must save it
The point I am making, Fatemeh, is that if you actually need the output of 'pdist2' for each possible combination of values of 'rsize' and 'k', it should be possible to quickly extract it in an appropriate manner from that single "maximum" run, rather than having to go through 'pdist2' calls repeatedly. Doing it that way should save a lot of time if you do it right. That "maximum" run contains all the pairs that you will need.
In all this discussion I am assuming that the matrices 'testset' and 'tmatrix' remain unchanged for each computation of 'pdist2', except that different portions of each are used. Is that correct? Otherwise the code you have shown wouldn't make sense.
yes the matrices 'testset' and 'tmatrix' remain unchanged for each computation of pdist2 but what you mean about maximum run?

Accedi per commentare.

Risposte (1)

Did you pre-allocate the result?
dis = zeros(ts, 1)
By the way, you overwrite the result dis in each iteration over rsize.
pdist2 is vectorized, so try to omit the loops:
dis = pdist2(testset(:, 1:57), tmatrix(:,1:57), 'euclidean');

2 Commenti

but i will need kth row of dis matrix at future

Accedi per commentare.

Richiesto:

il 9 Feb 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by