How to add multiple conditions to While loop ?
2 views (last 30 days)
Show older comments
Ararat Cetinkaya
on 7 Mar 2020
Commented: Ararat Cetinkaya
on 7 Mar 2020
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 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!