How to select one of two equal minimums in a matrix?

4 views (last 30 days)
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 Comments
Image Analyst
Image Analyst on 21 Jul 2017
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 on 21 Jul 2017
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

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 21 Jul 2017
‘... 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 on 22 Jul 2017
Edited: Image Analyst on 22 Jul 2017
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 Comments
Garrett
Garrett on 23 Jul 2017
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 on 23 Jul 2017
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?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!