while loop convergence problem
3 次查看(过去 30 天)
显示 更早的评论
i am writing a program to separate my mixed images using neural learning algorithm.i have a program instead of looping 100 times i want it to loop until (v*v') becomes identity matrix.
here's my code so far:-
clc;
clear all;
i1=imread('cameraman.tif');
i2=imresize(i1,[256 256]);
p1=reshape(i2,[1 65536]);
i3=imread('moon.tif');
i4=imresize(i3,[256 256]);
p2=reshape(i4,[1 65536]);
i5=imread('trees.tif');
i6=imresize(i5,[256 256]);
p3=reshape(i6,[1 65536]);
p=[p1;p2;p3];
a=unifrnd(-1,1,3,3);
mix=a*double(p);
m=mean(mix(:));
c=minus(mix,m);
c1=-1+2*((c-min(min(c)))/(max(max(c))-min(min((c)))));
n=.0000000001;
V=orth(unifrnd(-1,1,3,3));
I=eye(3,3);
con=0;
epoch=0;
while(con==0)
v=V*c1;
V=V+n*(I-v*v');
epoch=epoch+1;
if(epoch==2)
break;
end
end
kindly help me. thank you!
采纳的回答
Guillaume
2015-4-22
You just need to replace your if condition inside your loop.
while true
%... code that calculate V
if isequal(V*V', eye(size(V))
break;
end
end
Note that with the above will only stop the loop if V*V' is exactly the identity matrix. Small errors due to floating point precision may not make it true, so you may be better off with:
while true
%... code that calculate V
delta = V*V' - eye(size(V));
if max(abs(delta(:))) < 1e-10 %or whatever tolerance you want
break
end
end
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!