How to add multiple conditions to While loop ?

조회 수: 4 (최근 30일)
Ararat Cetinkaya
Ararat Cetinkaya 2020년 3월 7일
댓글: Ararat Cetinkaya 2020년 3월 7일
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

채택된 답변

Walter Roberson
Walter Roberson 2020년 3월 7일
Change the && to ||

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by