while loop convergence problem
3 views (last 30 days)
Show older comments
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!
Accepted Answer
Guillaume
on 22 Apr 2015
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 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!