Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Hungarian algortihm, serious trouble with loop! help!

6 views (last 30 days)
Ana
Ana on 6 Dec 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
So I'm trying to do the hungarian algorithm in matlab. I'm at the point where I have to "cross" the minimun amount of rows/columns making sure all the zeros in the matrix are crossed. So here is my matrix: A=[15 0 0 0;0 50 20 25;35 5 0 10;0 65 50 65]
and I already have vectors that account for the number of zeros not marked in each row and column: countl=[3 1 1 1] countc=[2 1 2 1] respectively
So I've written a loop, and in theory this is what I thinks needs to be done: Starting from the rows with less zeros, where we find a zero we put a 1 in the same position in a matrix of zeros (with the same dimension as A, let's call it maux). If we see that along the column where the first zero was is another zero we put in the same position in maux a -1. As we proceed the vectors countl and countc will decrease everytime we mark/find a new zero. When it ends, I should expect something like:
B=[0 1 -1 -1;-1 0 0 0;0 0 1 0;1 0 0 0]
I developed this code:
zer=zeros(1,n) while ~(isequal(countl,zer) && isequal(countc,zer))
[pmin]=findmin(countl);
for i=1:length(pmin)
for j=1:n
for w=1:n
not(i==w);
if A2(pmin(i),j)==0
maux(i,j)==1;
countl(i)=countl(i)-1;
countc(j)=countc(j)-1;
if A2(w,j)==0
maux(w,j)==-1;
countl(w)=countl(w)-1;
countc(j)=countc(j)-1;
end
end
end
end
end
end
function pmin=findmin(countl)
if min(countl)==0
countl(countl==0)=Inf;
pmin=find(countl==min(countl));
else
pmin=find(countl==min(countl));
end
end
Can someone help me? Bare in mind that I'm very inexperienced!
  1 Comment
Matt J
Matt J on 6 Dec 2012
Edited: Matt J on 6 Dec 2012
You haven't said what the problem is. What is not working in the code you've shown?

Answers (0)

This question is closed.

Community Treasure Hunt

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

Start Hunting!