How to add multiple conditions to While loop ?
2 次查看(过去 30 天)
显示 更早的评论
Hi everybody,
I want to work this While loop until all error values equal to 0, but code given below stop working when one of the error values equals to 0. How can I fix this problem?
aşağıdaki kodda error değeleri 0 olana dek while loopun çalışmasını istiyorum fakat error deperlerinden sadece biri tane 0 olduğunda loop duruyor
clc; clear all;
[num,txt,raw] = xlsread('data-1.xlsx');
%%
r = randi (1500,3,1); %%Randomly selecting a inital cluster
ci1 = [num(r(1,1),1) num(r(1,1),2)]; %% inital cluster centers
ci2 = [num(r(2,1),1) num(r(2,1),2)];
ci3 = [num(r(3,1),1) num(r(3,1),2)];
%%
error1 = 10;
error2 = 10;
error3 = 10;
%%
while error1 > 0 && error2 > 0 && error3 > 0
for i=1:1:1500
distance1 = sqrt(((ci1(1,1)-num(i,1))^2)+((ci1(1,2)-num(i,2))^2));
distance2 = sqrt(((ci2(1,1)-num(i,1))^2)+((ci2(1,2)-num(i,2))^2));
distance3 = sqrt(((ci3(1,1)-num(i,1))^2)+((ci3(1,2)-num(i,2))^2));
decision = [distance1(1,1),distance2(1,1),distance3(1,1)];
[M,I] = min(decision);
indices(i) = I;
end
%%
data = [num(:,1:2),indices'];
class1 = data(data(:,3)==1,1:2);
class2 = data(data(:,3)==2,1:2);
class3 = data(data(:,3)==3,1:2);
newcenter1 = [mean(class1(:,1)),mean(class1(:,2))];
newcenter2 = [mean(class2(:,1)),mean(class2(:,2))];
newcenter3 = [mean(class3(:,1)),mean(class3(:,2))];
%%
error1 = sqrt(((ci1(1,1)-newcenter1(1,1))^2)+((ci1(1,2)-newcenter1(1,2))^2));
error2 = sqrt(((ci2(1,1)-newcenter2(1,1))^2)+((ci2(1,2)-newcenter2(1,2))^2));
error3 = sqrt(((ci3(1,1)-newcenter3(1,1))^2)+((ci3(1,2)-newcenter3(1,2))^2));
ci1 = newcenter1;
ci2 = newcenter2;
ci3 = newcenter3;
end
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!