필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Hungarian algortihm, serious trouble with loop! help!

조회 수: 6 (최근 30일)
Ana
Ana 2012년 12월 6일
마감: MATLAB Answer Bot 2021년 8월 20일
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
Matt J
Matt J 2012년 12월 6일
편집: Matt J 2012년 12월 6일
You haven't said what the problem is. What is not working in the code you've shown?

답변 (0개)

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by