I need to know what's the problem with this code

Hello I need to know what's the problem with this code
% tedata contain 20 cells
% trdata contain 60 cells
for j=1:length(tedata)
for i=1:length(trdata)
y=cell2mat(tedata(j));
x=cell2mat(trdata(i));
similarity = sqrt(sum((x - y) .^ 2));
if similarity>180
similarity=similarity-360;
elseif similarity<-180
similarity=similarity+360;
end
kk(i)=similarity;
end
[result indxofmin] =min(kk);
but it return the value of similarity at first step [similarity = sqrt(sum((x - y) .^ 2)); ] not after the condition

4 件のコメント

dpb
dpb 2018 年 9 月 2 日
Some observations...
for j=1:length(tedata)
y=cell2mat(tedata(j)); % move out of _i_ loop; invariant w/ i
for i=1:length(trdata)
x=cell2mat(trdata(i));
similarity=norm(x-y); % use builtin function
if similarity > 180
similarity=similarity-360;
elseif similarity < -180
similarity=similarity+360;
end
kk(i)=similarity;
end
[result indxofmin] =min(kk);
Your code is missing closing end for j loop; presume is at end but you're not saving the result kk for each j; instead overwriting each loop.
similarity = sqrt(sum((x-y).^ 2));
or, more succinctly,
similarity = norm(x-y);
...
elseif similarity < -180
similarity=similarity+360;
similarity can never be <0; the square operation ensures is always positive, so that condition is totally superfluous.
As for the other, depending on x,y values, it's quite possible that norm(x-y) is never >180 so the condition is never met.
wisam kh
wisam kh 2018 年 9 月 2 日
#dpb
Thank you very much, things are clear now.
Image Analyst
Image Analyst 2018 年 9 月 8 日
Another major problem with it is that there are not enough comments in it. That would not be acceptable in our company.
wisam kh
wisam kh 2018 年 9 月 10 日
Ok thank you.

回答 (0 件)

この質問は閉じられています。

質問済み:

2018 年 9 月 2 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by