How to select one of two equal minimums in a matrix?
조회 수: 4 (최근 30일)
이전 댓글 표시
I am making a script to run a loop to get for minimum values and allocate resources to them, I eventually run into a situation where I find two equal non-zero minimums and the code stops since it makes them an array instead of a value. I need a way to have it randomly choose one of the two possible minimums. I don't know how to indicate this in the code. Be aware I am really new at this.
if true
CostsMtx=[16,18,17,20,17;25,27,29,32,28;1.5,1.6,1.7,2,1.8;50,54,56,60,57;60,63,65,68,64];
supply=[800,600,1000,400,100];
demand=[870,435,725,464,406];
mtxsz=size(CostsMtx);
%%%%%%%%%%
tmtx=CostsMtx;
res=zeros(mtxsz);
%%%%%%%%%
R=supply;
D=demand;
for i=1:7
x=min(tmtx(tmtx>0));
[Row,Col]=find(tmtx==x);
rm=min(R(1,Row));
dm=min(D(1,Col));
if R(1,Row)==0
tmtx(Row,Col)=0;
end
if D(1,Col)==0
tmtx(Row,Col)=0;
end
if rm<dm
res(Row,Col)=tmtx(Row,Col)*R(1,Row);
D(1,Col)=D(1,Col)-R(1,Row);
R(1,Row)=0;
tmtx(Row,Col)=0;
end
if dm<rm
res(Row,Col)=tmtx(Row,Col)*D(1,Col);
R(1,Row)=R(1,Row)-D(1,Col);
D(1,Col)=0;
tmtx(Row,Col)=0;
end
display(D);
display(R);
end
%%%%%%
댓글 수: 3
Image Analyst
2017년 7월 21일
It's difficult to visualize. I'm imagining you have a 2-D matrix with two local minimums, where the min values are not the same. If so a surface view might help us visualize. Or do you have a case where the mins are the same?
You forgot to give us CostsMtx so that's all I'm going to offer now until we get that data.
답변 (2개)
Star Strider
2017년 7월 21일
‘... and the code stops since it makes them an array instead of a value ...’
One option is to use ‘linear indexing’ and then randomly choose one index:
idx = find(tmtx==x);
... randomly choose one ‘idx’ value as ‘idx_wins’ ...
[Row,Col] = ind2sub(mtxsz, idx_wins);
or you could simply use ‘idx_wins’ as the linear index, and abandon the row and column references entirely. (I prefer linear indexing, since it is more likely to do what I want.)
See the documentation on the individual functions to understand their capabilities.
댓글 수: 0
Image Analyst
2017년 7월 22일
편집: Image Analyst
2017년 7월 22일
With this line:
res(Row,Col)=tmtx(Row,Col)*D(1,Col);
You're trying to do a matrix multiplication of a 2-by-2 by a 1-by-2. As you know from your linear algebra training, you can't do that.
We're not really sure what you want to do with the code because you either didn't include any comments or, stripped them all out before posting ( either action is very unwise).
댓글 수: 4
Image Analyst
2017년 7월 23일
I wish I knew what you wanted to do, but the cryptic variable names and lack of comments mean I'd have to spend too much time delving into this code to figure out what it actually does, and then try to make it do what you want it to do (which I don't know yet). What does "select one of two equal minimums in a matrix" mean? Why not just take the first one and be done with it?
참고 항목
카테고리
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!