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
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.
Garrett
Garrett 2017-7-21
Okay I added all of the script and it pops up with this: Error using * Inner matrix dimensions must agree.
Error in Untitled (line 31) res(Row,Col)=tmtx(Row,Col)*D(1,Col);
once I run it, It is because It is reading two points within the matrix that are =17, which it give all of the coordinates of both points which adds an extra dimension to a multiplier down below that it can't handle

请先登录,再进行评论。

回答(2 个)

Star Strider
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.

Image Analyst
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 个评论
Garrett
Garrett 2017-7-23
I sincerely apologize for the how confusing I am. In the first loops of the code that m1 and m2 are not multi dimensional arrays but rather single cell vectors. This is the issue it is reporting both the 17s as minimum when my code can only edit one value at a time
Image Analyst
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by