check if my algorithm works
1 view (last 30 days)
Show older comments
please can anyone check if my algorithm works or not? the goal of my algorithm is to decompose an image ento two parts using the non negative matrix factorization.
V= double(imread('circuit.tif'));
imshow(V);
[m,n]= size(V);
m = 280; n = 272; r = 200;
tol = 0.000001;
initt=cputime;
timelimit= 300;
Niter = 100;
w0 = abs (rand (m,r));h0=abs (rand(r,n));
W = w0; H = h0;
gradW = W*(H*H') - V*H';
gradH = (W'*W)*H - W'*V;
for iter=1:Niter,
if w0 == rand(m,r)
h0 = h0.* (V*w0')./((w0'*w0)*h0 + (10^-9))
gradW = W*(H*H') - V*H';
gradH = (W'*W)*H - W'*V;
end
if iter==1,
initgrad = norm([gradW; gradH'],'fro');
fprintf('init grad norm %f\n', initgrad);
end
projnorm = ([norm(gradW(gradW<0 | W>0)); norm(gradH(gradH<0 | H>0))]);
if projnorm < tol*initgrad | iter == Niter | cputime-initt > timelimit,
fprintf('Iter = %d Final proj-grad norm %f\n', iter, projnorm);
break
end
W = W.*(V*H')./(W*(H*H'))
H = H.*(W'*V)./((W'*W)*H)
if h0 == rand(r,n)
w0= w.*(V*h0')./(w0*(h0*h0')+10^-9);
gradW = W*(H*H') - V*H'
gradH = (W'*W)*H - W'*V
end
projnorm = ([norm(gradW(gradW<0 | W>0)); norm(gradH(gradH<0 | H>0))]);
if projnorm < tol*initgrad | iter == Niter | cputime-initt > timelimit;
fprintf('Iter = %d Final proj-grad norm %f\n', iter, projnorm);
break
end
W = W.*(V*H')./(W*(H*H'))
H = H.*(W'*V)./((W'*W)*H)
if iter== Niter,
fprintf('Max iter in nlssubprob\n');
end
end
I = double(mat2gray(W));
imshow(V, []),figure,imshow(I);
title('image decomposee');
2 Comments
John D'Errico
on 30 Oct 2016
Expecting someone to follow out your spaghetti code, and from that to figure out what you did and then verify that you did it properly is unlikely to get a serious answer.
It works if it does what you want. Conversely, if it generates garbage, then it probably does not work.
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!