How to fix a program that does not end?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I wrote a program for clustering that a number of kmeans algorithms are implemented on a dataset,
clear all
close all
clc
load lung.mat
a=lung;
K=3;
[n,m]=size(a);
NN=n;
m=m-1;
l=1;
l2=a(:,14); %clusters' labels (the last label for comparing)
%by using kmeans , input clusterings are achieved
%loops are for iterating 20 times for every k
for ii=1:20
for jj=1:l
e=kmean(a,m,n,K); % e: labels
cls(l,:)=e;
l1=e';
J(ii)=mutualinfo(l1,l2); % a function for comparing our resault with the main resault (Accuracy measure)
l=l+1;
end
K=K+1;
end
l=l-1;
%%%%%%%%%%%%%%%%%%%%%%
%5 times CSPA Algorithm
for i=1:5
c1=cspa(cls,k); % cls ( our automatic labels )
l1=c1';
I(i)=mutualinfo(l1,l2);
l=l+1;
cls(l,:)=c1;
end
It seams that there isn't any logical problems or errors in the codes but the program is always running and will not end.
I'll be very grateful to know your opinions about solving this issue and finding out the reasons of these kinds of errors.
Thank you
0 Commenti
Risposte (1)
Rik
il 18 Giu 2019
You are increasing your loop variable inside the loop:
l=l+1;
Is that intentional? What are you trying to achieve there? That inner loop will run over a million times.
Even if the functions you call there take about 200 ms to complete, that is still more than 58 hours.
clc
l=1;
for ii=1:20
fprintf('ii=%d\tl=%d\n',ii,l)
for jj=1:l
l=l+1;
end
end
ms=200;
fprintf('with a %d ms loop duration, this takes %.1f hours in total\n',...
ms,l*(ms/1000)/(3600))
>> with a 200 ms loop duration, this takes 58.3 hours
6 Commenti
Walter Roberson
il 19 Giu 2019
Yes, you should use zeros() to create the array.
l=1;
%...
for ii=1:20
for jj=1:l
When MATLAB encounters the for jj statement, it will record the current value of l and will use that value for the upper bound, not paying attention to the fact that l is changing in the loop. So the first time through, l is 1 so the statement is for jj=1:1
Inside that loop you have
l=l+1;
so when jj = 1:1 does its one iteration when ii is 1, then l will be incremented to 2.
Then ii becomes 2, and for jj=1:l looks at the current value of l and sees it is 2, so this becomes for jj=1:2 . Inside that loop you increment l, so l will be incremented to 3 and then 4.
Then ii becomes 3, and for jj=1:l looks at the current value of l and sees it is 4, so this becomes for jj=1:4 and you increment l each of those times, so l will become 5, 6, 7, 8.
If you think about this a moment, you can see that after any given for ii iteration, l will become 2^ii and the total number of iterations of the function will be sum of ii = 1 to 20 of 2^ii which is going to have a total of 2^21-1 iterations, which is 2097151 iterations. This then tells you what size you need to allocate for cls: for N iterations of ii it needs 2^N-1 rows, so you need
cls = zeros(2^20-1, SOMETHING)
I cannot make any guesses about what SOMETHING is. It depends on the size returned by your kmean function. There is no MATLAB function named kmean . Your comments refer to kmeans but kmeans does not take 4 inputs. If you were using kmeans(a,K) then the length of the output would be the same as the number of rows of a, which would be n in your program, so possibly
cls = zeros(2^20-1, n)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!