How to add multiple conditions to While loop ?

7 ビュー (過去 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 ||
  1 件のコメント
Ararat Cetinkaya
Ararat Cetinkaya 2020 年 3 月 7 日
I thought 'and' experssion was &&.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by